Skip to content

Commit b39021d

Browse files
committed
Merge branch 'pr2' into 1.x
2 parents bc8b836 + 505fdda commit b39021d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Swoole/SwooleClient.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
class SwooleClient implements Client, ServesStaticFiles
2121
{
22+
const STATUS_CODE_REASONS = [
23+
419 => 'Page Expired',
24+
];
25+
2226
public function __construct(protected int $chunkSize = 1048576)
2327
{
2428
}
@@ -176,7 +180,11 @@ public function sendResponseHeaders(Response $response, SwooleResponse $swooleRe
176180
}
177181
}
178182

179-
$swooleResponse->status($response->getStatusCode());
183+
if (! is_null($reason = $this->getReasonFromStatusCode($response->getStatusCode()))) {
184+
$swooleResponse->status($response->getStatusCode(), $reason);
185+
} else {
186+
$swooleResponse->status($response->getStatusCode());
187+
}
180188

181189
foreach ($response->headers->getCookies() as $cookie) {
182190
$swooleResponse->{$cookie->isRaw() ? 'rawcookie' : 'cookie'}(
@@ -265,4 +273,19 @@ public function error(Throwable $e, Application $app, Request $request, RequestC
265273
Octane::formatExceptionForClient($e, $app->make('config')->get('app.debug'))
266274
);
267275
}
276+
277+
/**
278+
* Get the HTTP reason clause for non-standard status codes.
279+
*
280+
* @param int $code
281+
* @return string|null
282+
*/
283+
protected function getReasonFromStatusCode(int $code): ?string
284+
{
285+
if (array_key_exists($code, self::STATUS_CODE_REASONS)) {
286+
return self::STATUS_CODE_REASONS[$code];
287+
}
288+
289+
return null;
290+
}
268291
}

tests/SwooleClientTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ public function test_respond_method_send_streamed_response_to_swoole()
181181
}, 200, ['Content-Type' => 'text/html'])));
182182
}
183183

184+
/** @doesNotPerformAssertions @test */
185+
public function test_respond_method_with_laravel_specific_status_code_sends_response_to_swoole()
186+
{
187+
$client = new SwooleClient;
188+
189+
$swooleResponse = Mockery::mock('Swoole\Http\Response');
190+
191+
$swooleResponse->shouldReceive('status')->once()->with(419, 'Page Expired');
192+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
193+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
194+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
195+
$swooleResponse->shouldReceive('write')->with('Hello World');
196+
$swooleResponse->shouldReceive('end')->once();
197+
198+
$client->respond(new RequestContext([
199+
'swooleResponse' => $swooleResponse,
200+
]), new OctaneResponse(new Response('Hello World', 419, ['Content-Type' => 'text/html'])));
201+
}
202+
184203
/** @doesNotPerformAssertions @test */
185204
public function test_error_method_sends_error_response_to_swoole()
186205
{

0 commit comments

Comments
 (0)