diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index a0e25bb4a..9578997e2 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -261,11 +261,13 @@ protected function sendResponseContent(OctaneResponse $octaneResponse, SwooleRes } if ($length <= $this->chunkSize) { - $swooleResponse->write($content); - } else { - for ($offset = 0; $offset < $length; $offset += $this->chunkSize) { - $swooleResponse->write(substr($content, $offset, $this->chunkSize)); - } + $swooleResponse->end($content); + + return; + } + + for ($offset = 0; $offset < $length; $offset += $this->chunkSize) { + $swooleResponse->write(substr($content, $offset, $this->chunkSize)); } $swooleResponse->end(); diff --git a/tests/SwooleClientTest.php b/tests/SwooleClientTest.php index 6547357fd..43570ac9e 100644 --- a/tests/SwooleClientTest.php +++ b/tests/SwooleClientTest.php @@ -9,6 +9,7 @@ use Laravel\Octane\RequestContext; use Laravel\Octane\Swoole\SwooleClient; use Mockery; +use Swoole\Http\Response as SwooleResponse; use Symfony\Component\HttpFoundation\StreamedResponse; class SwooleClientTest extends TestCase @@ -280,4 +281,47 @@ public function test_error_method_sends_detailed_error_response_to_swoole_in_deb $swooleResponse->shouldHaveReceived('header')->with('Content-Type', 'text/plain'); $swooleResponse->shouldHaveReceived('end')->with((string) $e); } + + /** @doesNotPerformAssertions @test */ + public function test_respond_method_send_not_chunked_response_to_swoole(): void + { + $client = new SwooleClient; + + $swooleResponse = Mockery::mock(SwooleResponse::class); + + $swooleResponse->shouldReceive('status')->once()->with(200); + $swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private'); + $swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html'); + $swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string')); + $swooleResponse->shouldReceive('write')->never(); + $swooleResponse->shouldReceive('end')->once()->with('Hello World'); + + $response = new Response('Hello World', 200, ['Content-Type' => 'text/html']); + + $client->respond(new RequestContext([ + 'swooleResponse' => $swooleResponse, + ]), new OctaneResponse($response)); + } + + /** @doesNotPerformAssertions @test */ + public function test_respond_method_send_chunked_response_to_swoole(): void + { + $client = new SwooleClient(6); + + $swooleResponse = Mockery::mock('Swoole\Http\Response'); + + $swooleResponse->shouldReceive('status')->once()->with(200); + $swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private'); + $swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html'); + $swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string')); + $swooleResponse->shouldReceive('write')->once()->with('Hello '); + $swooleResponse->shouldReceive('write')->once()->with('World'); + $swooleResponse->shouldReceive('end')->once(); + + $response = new Response('Hello World', 200, ['Content-Type' => 'text/html']); + + $client->respond(new RequestContext([ + 'swooleResponse' => $swooleResponse, + ]), new OctaneResponse($response)); + } }