From 9753469f28b3018b713e7f5aad1a150f55dec3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 17 Apr 2024 20:41:31 +0800 Subject: [PATCH] Fixed bug that `withoutBody` cannot not work when using `Swow`. (#6697) --- src/Base/Request.php | 11 +++++------ src/Base/Response.php | 2 +- src/Server/ResponsePlusProxy.php | 2 +- tests/ResponseTest.php | 25 +++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Base/Request.php b/src/Base/Request.php index 0491a6c..a530a72 100755 --- a/src/Base/Request.php +++ b/src/Base/Request.php @@ -246,20 +246,19 @@ public function setRequestTarget(string $requestTarget): static public function toString(bool $withoutBody = false): string { $headerString = ''; - if (! $withoutBody) { - foreach ($this->getStandardHeaders() as $key => $values) { - foreach ($values as $value) { - $headerString .= sprintf("%s: %s\r\n", $key, $value); - } + foreach ($this->getStandardHeaders() as $key => $values) { + foreach ($values as $value) { + $headerString .= sprintf("%s: %s\r\n", $key, $value); } } + return sprintf( "%s %s HTTP/%s\r\n%s\r\n%s", $this->getMethod(), $this->getUri()->getPath(), $this->getProtocolVersion(), $headerString, - $this->getBody() + $withoutBody ? '' : $this->getBody() ); } diff --git a/src/Base/Response.php b/src/Base/Response.php index 7b7b79d..798f1e1 100755 --- a/src/Base/Response.php +++ b/src/Base/Response.php @@ -354,7 +354,7 @@ public function toString(bool $withoutBody = false): string $this->getStatusCode(), $this->getReasonPhrase(), $headerString, - $this->getBody() + $withoutBody ? '' : $this->getBody() ); } diff --git a/src/Server/ResponsePlusProxy.php b/src/Server/ResponsePlusProxy.php index 7dbccd3..8d9ad1c 100644 --- a/src/Server/ResponsePlusProxy.php +++ b/src/Server/ResponsePlusProxy.php @@ -181,7 +181,7 @@ public function toString(bool $withoutBody = false): string $this->getStatusCode(), $this->getReasonPhrase(), $headerString, - $this->getBody() + $withoutBody ? '' : $this->getBody() ); } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 57ff3b5..38e99ed 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -12,13 +12,16 @@ namespace HyperfTest\HttpMessage; +use Hyperf\Codec\Json; use Hyperf\Engine\Http\WritableConnection; use Hyperf\HttpMessage\Cookie\Cookie; use Hyperf\HttpMessage\Server\Response; +use Hyperf\HttpMessage\Stream\SwooleStream; use Mockery; use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Swoole\Http\Response as SwooleResponse; +use Swow\Psr7\Message\ResponsePlusInterface; /** * @internal @@ -70,6 +73,28 @@ public function testWrite() $this->assertTrue($status); } + public function testToString() + { + $response = $this->newResponse(); + if (! $response instanceof ResponsePlusInterface) { + $this->markTestSkipped('Don\'t assert response which not instanceof ResponsePlusInterface'); + } + + $response->setStatus(200)->setHeaders(['Content-Type' => 'application/json'])->setBody(new SwooleStream(Json::encode(['id' => $id = uniqid()]))); + $this->assertEquals("HTTP/1.1 200 OK\r +Content-Type: application/json\r +Connection: close\r +Content-Length: 22\r +\r +{\"id\":\"" . $id . '"}', $response->toString()); + $this->assertSame("HTTP/1.1 200 OK\r +Content-Type: application/json\r +Connection: close\r +Content-Length: 22\r +\r +", $response->toString(true)); + } + protected function newResponse() { return new Response();