From 1d73f6ad22a6d42fbe20dc50ca5fd5b82e405bee Mon Sep 17 00:00:00 2001 From: Saif M Date: Thu, 27 May 2021 09:08:08 +0600 Subject: [PATCH 1/2] Fixes issue related to changing non-standard HTTP status codes to 200 OK --- src/Swoole/SwooleClient.php | 25 ++++++++++++++++++++++++- tests/SwooleClientTest.php | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index 8d0a0fe99..1882e173c 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -19,6 +19,10 @@ class SwooleClient implements Client, ServesStaticFiles { + const STATUS_CODE_REASONS = [ + 419 => 'Page Expired', + ]; + public function __construct(protected int $chunkSize = 1048576) { } @@ -176,7 +180,11 @@ public function sendResponseHeaders(Response $response, SwooleResponse $swooleRe } } - $swooleResponse->status($response->getStatusCode()); + if (! is_null($reason = $this->getReasonFromStatusCode($response->getStatusCode()))) { + $swooleResponse->status($response->getStatusCode(), $reason); + } else { + $swooleResponse->status($response->getStatusCode()); + } foreach ($response->headers->getCookies() as $cookie) { $swooleResponse->{$cookie->isRaw() ? 'rawcookie' : 'cookie'}( @@ -265,4 +273,19 @@ public function error(Throwable $e, Application $app, Request $request, RequestC Octane::formatExceptionForClient($e, $app->make('config')->get('app.debug')) ); } + + /** + * Get the HTTP reason clause for non-standard status codes. + * + * @param int $code + * @return string|null + */ + protected function getReasonFromStatusCode(int $code): ?string + { + if (array_key_exists($code, self::STATUS_CODE_REASONS)) { + return self::STATUS_CODE_REASONS[$code]; + } + + return null; + } } diff --git a/tests/SwooleClientTest.php b/tests/SwooleClientTest.php index 6ff3ad24e..1f4447976 100644 --- a/tests/SwooleClientTest.php +++ b/tests/SwooleClientTest.php @@ -181,6 +181,25 @@ public function test_respond_method_send_streamed_response_to_swoole() }, 200, ['Content-Type' => 'text/html']))); } + /** @doesNotPerformAssertions @test */ + public function test_respond_method_with_laravel_specific_status_code_sends_response_to_swoole() + { + $client = new SwooleClient; + + $swooleResponse = Mockery::mock('Swoole\Http\Response'); + + $swooleResponse->shouldReceive('status')->once()->with(419, 'Page Expired'); + $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')->with('Hello World'); + $swooleResponse->shouldReceive('end')->once(); + + $client->respond(new RequestContext([ + 'swooleResponse' => $swooleResponse, + ]), new OctaneResponse(new Response('Hello World', 419, ['Content-Type' => 'text/html']))); + } + /** @doesNotPerformAssertions @test */ public function test_error_method_sends_error_response_to_swoole() { From 505fddafb63f9f01007314b6e64dabd8011bd81d Mon Sep 17 00:00:00 2001 From: Saif M Date: Thu, 27 May 2021 09:17:18 +0600 Subject: [PATCH 2/2] Formatting --- src/Swoole/SwooleClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index 1882e173c..50eb89e67 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -277,7 +277,7 @@ public function error(Throwable $e, Application $app, Request $request, RequestC /** * Get the HTTP reason clause for non-standard status codes. * - * @param int $code + * @param int $code * @return string|null */ protected function getReasonFromStatusCode(int $code): ?string