Skip to content

Commit 9753469

Browse files
authored
Fixed bug that withoutBody cannot not work when using Swow. (#6697)
1 parent 609dfb8 commit 9753469

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/Base/Request.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,19 @@ public function setRequestTarget(string $requestTarget): static
246246
public function toString(bool $withoutBody = false): string
247247
{
248248
$headerString = '';
249-
if (! $withoutBody) {
250-
foreach ($this->getStandardHeaders() as $key => $values) {
251-
foreach ($values as $value) {
252-
$headerString .= sprintf("%s: %s\r\n", $key, $value);
253-
}
249+
foreach ($this->getStandardHeaders() as $key => $values) {
250+
foreach ($values as $value) {
251+
$headerString .= sprintf("%s: %s\r\n", $key, $value);
254252
}
255253
}
254+
256255
return sprintf(
257256
"%s %s HTTP/%s\r\n%s\r\n%s",
258257
$this->getMethod(),
259258
$this->getUri()->getPath(),
260259
$this->getProtocolVersion(),
261260
$headerString,
262-
$this->getBody()
261+
$withoutBody ? '' : $this->getBody()
263262
);
264263
}
265264

src/Base/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function toString(bool $withoutBody = false): string
354354
$this->getStatusCode(),
355355
$this->getReasonPhrase(),
356356
$headerString,
357-
$this->getBody()
357+
$withoutBody ? '' : $this->getBody()
358358
);
359359
}
360360

src/Server/ResponsePlusProxy.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function toString(bool $withoutBody = false): string
181181
$this->getStatusCode(),
182182
$this->getReasonPhrase(),
183183
$headerString,
184-
$this->getBody()
184+
$withoutBody ? '' : $this->getBody()
185185
);
186186
}
187187

tests/ResponseTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212

1313
namespace HyperfTest\HttpMessage;
1414

15+
use Hyperf\Codec\Json;
1516
use Hyperf\Engine\Http\WritableConnection;
1617
use Hyperf\HttpMessage\Cookie\Cookie;
1718
use Hyperf\HttpMessage\Server\Response;
19+
use Hyperf\HttpMessage\Stream\SwooleStream;
1820
use Mockery;
1921
use PHPUnit\Framework\Attributes\CoversNothing;
2022
use PHPUnit\Framework\TestCase;
2123
use Swoole\Http\Response as SwooleResponse;
24+
use Swow\Psr7\Message\ResponsePlusInterface;
2225

2326
/**
2427
* @internal
@@ -70,6 +73,28 @@ public function testWrite()
7073
$this->assertTrue($status);
7174
}
7275

76+
public function testToString()
77+
{
78+
$response = $this->newResponse();
79+
if (! $response instanceof ResponsePlusInterface) {
80+
$this->markTestSkipped('Don\'t assert response which not instanceof ResponsePlusInterface');
81+
}
82+
83+
$response->setStatus(200)->setHeaders(['Content-Type' => 'application/json'])->setBody(new SwooleStream(Json::encode(['id' => $id = uniqid()])));
84+
$this->assertEquals("HTTP/1.1 200 OK\r
85+
Content-Type: application/json\r
86+
Connection: close\r
87+
Content-Length: 22\r
88+
\r
89+
{\"id\":\"" . $id . '"}', $response->toString());
90+
$this->assertSame("HTTP/1.1 200 OK\r
91+
Content-Type: application/json\r
92+
Connection: close\r
93+
Content-Length: 22\r
94+
\r
95+
", $response->toString(true));
96+
}
97+
7398
protected function newResponse()
7499
{
75100
return new Response();

0 commit comments

Comments
 (0)