Skip to content

Commit

Permalink
Merge branch 'pr2' into 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 27, 2021
2 parents bc8b836 + 505fdda commit b39021d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Swoole/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

class SwooleClient implements Client, ServesStaticFiles
{
const STATUS_CODE_REASONS = [
419 => 'Page Expired',
];

public function __construct(protected int $chunkSize = 1048576)
{
}
Expand Down Expand Up @@ -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'}(
Expand Down Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions tests/SwooleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b39021d

Please sign in to comment.