Skip to content

Commit

Permalink
Fixed bug that withoutBody cannot not work when using Swow. (#6697)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored Apr 17, 2024
1 parent 609dfb8 commit 9753469
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/Base/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Base/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function toString(bool $withoutBody = false): string
$this->getStatusCode(),
$this->getReasonPhrase(),
$headerString,
$this->getBody()
$withoutBody ? '' : $this->getBody()
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Server/ResponsePlusProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function toString(bool $withoutBody = false): string
$this->getStatusCode(),
$this->getReasonPhrase(),
$headerString,
$this->getBody()
$withoutBody ? '' : $this->getBody()
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 9753469

Please sign in to comment.