Skip to content

Commit

Permalink
Fix call to undefined method when download file
Browse files Browse the repository at this point in the history
  • Loading branch information
bepsvpt committed Apr 9, 2017
1 parent 0761304 commit 5b7ccd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/SecureHeadersMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class SecureHeadersMiddleware
{
Expand All @@ -20,6 +21,11 @@ public function handle(Request $request, Closure $next)
{
$response = $next($request);

// when response is BinaryFileResponse, we should not add headers
if ($response instanceof BinaryFileResponse) {
return $response;
}

$headers = (new SecureHeaders(config('secure-headers', [])))->headers();

foreach ($headers as $key => $value) {
Expand Down
16 changes: 16 additions & 0 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@ protected function getEnvironmentSetUp($app)

public function test_middleware()
{
$this->app['router']->get('/', function () {
return 'Hello World!';
});

$response = $this->get('/');

$response->assertHeader('x-frame-options');
$response->assertHeader('content-security-policy');
}

public function test_binary_response()
{
$this->app['router']->get('/', function () {
return response()->download(__DIR__.'/../README.md');
});

$response = $this->get('/');

$this->assertFalse($response->headers->has('x-content-type-options'));
$this->assertFalse($response->headers->has('content-security-policy'));
}
}

0 comments on commit 5b7ccd3

Please sign in to comment.