Skip to content

Commit

Permalink
Merge pull request #103 from laravel/fix/octane-file-responses
Browse files Browse the repository at this point in the history
[2.x] Fixes file / download responses with Octane
  • Loading branch information
nunomaduro authored Oct 14, 2021
2 parents 13fd1cf + 046454a commit e6982c3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Runtime/Octane/Octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/LoadBalancedOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/OctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log();

0 comments on commit e6982c3

Please sign in to comment.