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

[9.x] Fix middleware SetCacheHeaders with file responses #44063

Merged
merged 2 commits into from
Sep 9, 2022
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
3 changes: 2 additions & 1 deletion src/Illuminate/Http/Middleware/SetCacheHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Carbon;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class SetCacheHeaders
{
Expand All @@ -21,7 +22,7 @@ public function handle($request, Closure $next, $options = [])
{
$response = $next($request);

if (! $request->isMethodCacheable() || ! $response->getContent()) {
if (! $request->isMethodCacheable() || (! $response->getContent() && ! $response instanceof BinaryFileResponse)) {
return $response;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Http/Middleware/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Carbon;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CacheTest extends TestCase
{
Expand All @@ -33,6 +34,16 @@ public function testDoNotSetHeaderWhenNoContent()
$this->assertNull($response->getEtag());
}

public function testSetHeaderToFileEvenWithNoContent()
{
$response = (new Cache)->handle(new Request, function () {
$filePath = __DIR__.'/../fixtures/test.txt';
return new BinaryFileResponse($filePath);
}, 'max_age=120;s_maxage=60');

$this->assertNotNull($response->getMaxAge());
}

public function testAddHeaders()
{
$response = (new Cache)->handle(new Request, function () {
Expand Down