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

[2.0] Fixes streamed responses when using Octane #159

Merged
merged 1 commit into from
Sep 6, 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
18 changes: 18 additions & 0 deletions src/Runtime/Octane/Octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Laravel\Vapor\Runtime\StorageDirectories;
use PDO;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;

class Octane implements Client
Expand Down Expand Up @@ -166,6 +167,10 @@ public static function handle($request)
? $response->getFile()->getContent()
: $response->getContent();

if ($response instanceof StreamedResponse) {
$content = static::captureContent($response);
}

return tap(new Response(
$content,
$response->headers->all(),
Expand Down Expand Up @@ -284,4 +289,17 @@ public static function worker()
{
return static::$worker;
}

/**
* Capture the content from a streamed response.
*/
protected static function captureContent(StreamedResponse $response): string
{
ob_start();
$response->sendContent();
$content = ob_get_contents();
ob_end_clean();

return $content;
}
}
21 changes: 21 additions & 0 deletions tests/Feature/ApiGatewayOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laravel\Vapor\Tests\TestCase;
use Mockery;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ApiGatewayOctaneHandlerTest extends TestCase
{
Expand Down Expand Up @@ -601,4 +602,24 @@ public function test_maintenance_mode_with_invalid_bypass_cookie()
static::assertEquals('application/json', $response->toApiGatewayFormat()['headers']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_streamed_responses()
{
$handler = new OctaneHandler();

Route::get('/', function () {
return new StreamedResponse(function () {
echo 'Hello World';
}, 200, ['mime-type' => 'image/png']);
});

$response = $handler->handle([
'httpMethod' => 'GET',
'path' => '/',
'headers' => [],
]);

static::assertEquals('image/png', $response->toApiGatewayFormat()['headers']['Mime-Type']);
static::assertEquals('Hello World', $response->toApiGatewayFormat()['body']);
}
}
23 changes: 23 additions & 0 deletions tests/Feature/LoadBalancedOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laravel\Vapor\Tests\TestCase;
use Mockery;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;

class LoadBalancedOctaneHandlerTest extends TestCase
{
Expand Down Expand Up @@ -599,4 +600,26 @@ public function test_maintenance_mode_with_invalid_bypass_cookie()
static::assertEquals(['application/json'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_streamed_responses()
{
$handler = new LoadBalancedOctaneHandler();

Route::get('/', function () {
return new StreamedResponse(function () {
echo 'Hello World';
}, 200, ['mime-type' => 'image/png']);
});

$response = $handler->handle([
'httpMethod' => 'GET',
'path' => '/',
'headers' => [
'Accept' => 'application/json',
],
]);

static::assertEquals(['image/png'], $response->toApiGatewayFormat()['multiValueHeaders']['Mime-Type']);
static::assertEquals('Hello World', $response->toApiGatewayFormat()['body']);
}
}
Loading