diff --git a/src/Runtime/Octane/Octane.php b/src/Runtime/Octane/Octane.php index e43b93e..0a69792 100644 --- a/src/Runtime/Octane/Octane.php +++ b/src/Runtime/Octane/Octane.php @@ -22,6 +22,7 @@ use Laravel\Vapor\Runtime\Response; use Laravel\Vapor\Runtime\StorageDirectories; use PDO; +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Throwable; class Octane implements Client @@ -166,8 +167,12 @@ public static function handle($request) }); } + $content = $response instanceof BinaryFileResponse + ? $response->getFile()->getContent() + : $response->getContent(); + return tap(new Response( - $response->getContent(), + $content, $response->headers->all(), $response->getStatusCode() ), static function () { diff --git a/tests/Feature/LoadBalancedOctaneHandlerTest.php b/tests/Feature/LoadBalancedOctaneHandlerTest.php index b50deff..aba8ba3 100644 --- a/tests/Feature/LoadBalancedOctaneHandlerTest.php +++ b/tests/Feature/LoadBalancedOctaneHandlerTest.php @@ -84,6 +84,42 @@ public function test_response_fires_events() Event::assertDispatched(RequestTerminated::class); } + public function test_response_file() + { + $handler = new LoadBalancedOctaneHandler(); + + Route::get('/', function (Request $request) { + return response()->file(__DIR__.'/../Fixtures/asset.js', [ + 'Content-Type' => 'text/javascript', + ]); + }); + + $response = $handler->handle([ + 'httpMethod' => 'GET', + 'path' => '/', + ]); + + self::assertEquals(['text/javascript'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']); + self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']); + } + + public function test_response_download() + { + $handler = new LoadBalancedOctaneHandler(); + + Route::get('/', function (Request $request) { + return response()->download(__DIR__.'/../Fixtures/asset.js'); + }); + + $response = $handler->handle([ + 'httpMethod' => 'GET', + 'path' => '/', + ]); + + self::assertEquals(['attachment; filename=asset.js'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Disposition']); + self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']); + } + public function test_response_headers() { $handler = new LoadBalancedOctaneHandler(); diff --git a/tests/Feature/OctaneHandlerTest.php b/tests/Feature/OctaneHandlerTest.php index 78c51e7..790c729 100644 --- a/tests/Feature/OctaneHandlerTest.php +++ b/tests/Feature/OctaneHandlerTest.php @@ -64,6 +64,42 @@ public function test_response_body() static::assertEquals('Hello World', $response->toApiGatewayFormat()['body']); } + public function test_response_file() + { + $handler = new OctaneHandler(); + + Route::get('/', function (Request $request) { + return response()->file(__DIR__.'/../Fixtures/asset.js', [ + 'Content-Type' => 'text/javascript', + ]); + }); + + $response = $handler->handle([ + 'httpMethod' => 'GET', + 'path' => '/', + ]); + + self::assertEquals('text/javascript', $response->toApiGatewayFormat()['headers']['Content-Type']); + self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']); + } + + public function test_response_download() + { + $handler = new OctaneHandler(); + + Route::get('/', function (Request $request) { + return response()->download(__DIR__.'/../Fixtures/asset.js'); + }); + + $response = $handler->handle([ + 'httpMethod' => 'GET', + 'path' => '/', + ]); + + self::assertEquals('attachment; filename=asset.js', $response->toApiGatewayFormat()['headers']['Content-Disposition']); + self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']); + } + public function test_response_fires_events() { Event::fake([RequestReceived::class, RequestTerminated::class]); diff --git a/tests/Fixtures/asset.js b/tests/Fixtures/asset.js new file mode 100644 index 0000000..b7bd4c8 --- /dev/null +++ b/tests/Fixtures/asset.js @@ -0,0 +1 @@ +console.log();