Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable http compression by sending data not with chunked encodi… #691

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Swoole/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 44 additions & 0 deletions tests/SwooleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}