diff --git a/composer.json b/composer.json index e74345c..1608611 100755 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "require": { "php": ">=8.1", "hyperf/codec": "~3.1.0", - "hyperf/engine": "^2.7", + "hyperf/engine": "^2.11", "hyperf/support": "~3.1.0", "laminas/laminas-mime": "^2.7", "psr/http-message": "^1.0|^2.0", diff --git a/src/Base/Request.php b/src/Base/Request.php index a530a72..81e3d5c 100755 --- a/src/Base/Request.php +++ b/src/Base/Request.php @@ -12,6 +12,7 @@ namespace Hyperf\HttpMessage\Base; +use Hyperf\Engine\Http\Http; use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Uri\Uri; use InvalidArgumentException; @@ -245,20 +246,12 @@ public function setRequestTarget(string $requestTarget): static public function toString(bool $withoutBody = false): string { - $headerString = ''; - 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", + return Http::packRequest( $this->getMethod(), $this->getUri()->getPath(), - $this->getProtocolVersion(), - $headerString, - $withoutBody ? '' : $this->getBody() + $this->getStandardHeaders(), + $withoutBody ? '' : (string) $this->getBody(), + $this->getProtocolVersion() ); } diff --git a/src/Base/Response.php b/src/Base/Response.php index 798f1e1..b989772 100755 --- a/src/Base/Response.php +++ b/src/Base/Response.php @@ -12,6 +12,7 @@ namespace Hyperf\HttpMessage\Base; +use Hyperf\Engine\Http\Http; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Stringable; @@ -342,19 +343,12 @@ public function isEmpty(): bool public function toString(bool $withoutBody = false): string { - $headerString = ''; - foreach ($this->getStandardHeaders() as $key => $values) { - foreach ($values as $value) { - $headerString .= sprintf("%s: %s\r\n", $key, $value); - } - } - return sprintf( - "HTTP/%s %s %s\r\n%s\r\n%s", - $this->getProtocolVersion(), + return Http::packResponse( $this->getStatusCode(), $this->getReasonPhrase(), - $headerString, - $withoutBody ? '' : $this->getBody() + $this->getStandardHeaders(), + $withoutBody ? '' : (string) $this->getBody(), + $this->getProtocolVersion() ); } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 38e99ed..b467552 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -15,8 +15,10 @@ use Hyperf\Codec\Json; use Hyperf\Engine\Http\WritableConnection; use Hyperf\HttpMessage\Cookie\Cookie; +use Hyperf\HttpMessage\Server\Request; use Hyperf\HttpMessage\Server\Response; use Hyperf\HttpMessage\Stream\SwooleStream; +use Hyperf\HttpMessage\Uri\Uri; use Mockery; use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; @@ -73,7 +75,7 @@ public function testWrite() $this->assertTrue($status); } - public function testToString() + public function testToResponseString() { $response = $this->newResponse(); if (! $response instanceof ResponsePlusInterface) { @@ -95,6 +97,25 @@ public function testToString() ", $response->toString(true)); } + public function testToRequestString() + { + $request = new Request('GET', new Uri('https://www.baidu.com/'), body: 'q=Hyperf'); + + $this->assertSame("GET / HTTP/1.1\r +host: www.baidu.com\r +Connection: close\r +Content-Length: 8\r +\r +q=Hyperf", $request->toString()); + + $this->assertSame("GET / HTTP/1.1\r +host: www.baidu.com\r +Connection: close\r +Content-Length: 8\r +\r +", $request->toString(true)); + } + protected function newResponse() { return new Response(); diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index 000284e..c276a4c 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -40,10 +40,6 @@ * @coversNothing */ #[CoversNothing] -/** - * @internal - * @coversNothing - */ class ServerRequestTest extends TestCase { protected function tearDown(): void