Skip to content

Commit

Permalink
allow streamed responses on octane (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon authored Sep 6, 2023
1 parent 4a39228 commit 12df8de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
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']);
}
}

0 comments on commit 12df8de

Please sign in to comment.