Skip to content

Commit 2568cc3

Browse files
feat: enable http compression by sending data not with chunked encodi… (#691)
* feat: enable http compression by sending data not with chunked encoding - if possible * Update SwooleClient.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent f827cc8 commit 2568cc3

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/Swoole/SwooleClient.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,13 @@ protected function sendResponseContent(OctaneResponse $octaneResponse, SwooleRes
261261
}
262262

263263
if ($length <= $this->chunkSize) {
264-
$swooleResponse->write($content);
265-
} else {
266-
for ($offset = 0; $offset < $length; $offset += $this->chunkSize) {
267-
$swooleResponse->write(substr($content, $offset, $this->chunkSize));
268-
}
264+
$swooleResponse->end($content);
265+
266+
return;
267+
}
268+
269+
for ($offset = 0; $offset < $length; $offset += $this->chunkSize) {
270+
$swooleResponse->write(substr($content, $offset, $this->chunkSize));
269271
}
270272

271273
$swooleResponse->end();

tests/SwooleClientTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Laravel\Octane\RequestContext;
1010
use Laravel\Octane\Swoole\SwooleClient;
1111
use Mockery;
12+
use Swoole\Http\Response as SwooleResponse;
1213
use Symfony\Component\HttpFoundation\StreamedResponse;
1314

1415
class SwooleClientTest extends TestCase
@@ -280,4 +281,47 @@ public function test_error_method_sends_detailed_error_response_to_swoole_in_deb
280281
$swooleResponse->shouldHaveReceived('header')->with('Content-Type', 'text/plain');
281282
$swooleResponse->shouldHaveReceived('end')->with((string) $e);
282283
}
284+
285+
/** @doesNotPerformAssertions @test */
286+
public function test_respond_method_send_not_chunked_response_to_swoole(): void
287+
{
288+
$client = new SwooleClient;
289+
290+
$swooleResponse = Mockery::mock(SwooleResponse::class);
291+
292+
$swooleResponse->shouldReceive('status')->once()->with(200);
293+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
294+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
295+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
296+
$swooleResponse->shouldReceive('write')->never();
297+
$swooleResponse->shouldReceive('end')->once()->with('Hello World');
298+
299+
$response = new Response('Hello World', 200, ['Content-Type' => 'text/html']);
300+
301+
$client->respond(new RequestContext([
302+
'swooleResponse' => $swooleResponse,
303+
]), new OctaneResponse($response));
304+
}
305+
306+
/** @doesNotPerformAssertions @test */
307+
public function test_respond_method_send_chunked_response_to_swoole(): void
308+
{
309+
$client = new SwooleClient(6);
310+
311+
$swooleResponse = Mockery::mock('Swoole\Http\Response');
312+
313+
$swooleResponse->shouldReceive('status')->once()->with(200);
314+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
315+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
316+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
317+
$swooleResponse->shouldReceive('write')->once()->with('Hello ');
318+
$swooleResponse->shouldReceive('write')->once()->with('World');
319+
$swooleResponse->shouldReceive('end')->once();
320+
321+
$response = new Response('Hello World', 200, ['Content-Type' => 'text/html']);
322+
323+
$client->respond(new RequestContext([
324+
'swooleResponse' => $swooleResponse,
325+
]), new OctaneResponse($response));
326+
}
283327
}

0 commit comments

Comments
 (0)