Skip to content

Commit cbeb50b

Browse files
Fix middleware "SetCacheHeaders" with file responses (#44063)
* Fix test is content empty when it is a file response because in this case, it is always empty * add test for cache header with file
1 parent 1b20ec4 commit cbeb50b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Illuminate/Http/Middleware/SetCacheHeaders.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Support\Carbon;
7+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
78

89
class SetCacheHeaders
910
{
@@ -21,7 +22,7 @@ public function handle($request, Closure $next, $options = [])
2122
{
2223
$response = $next($request);
2324

24-
if (! $request->isMethodCacheable() || ! $response->getContent()) {
25+
if (! $request->isMethodCacheable() || (! $response->getContent() && ! $response instanceof BinaryFileResponse)) {
2526
return $response;
2627
}
2728

tests/Http/Middleware/CacheTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Carbon;
99
use InvalidArgumentException;
1010
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1112

1213
class CacheTest extends TestCase
1314
{
@@ -33,6 +34,16 @@ public function testDoNotSetHeaderWhenNoContent()
3334
$this->assertNull($response->getEtag());
3435
}
3536

37+
public function testSetHeaderToFileEvenWithNoContent()
38+
{
39+
$response = (new Cache)->handle(new Request, function () {
40+
$filePath = __DIR__.'/../fixtures/test.txt';
41+
return new BinaryFileResponse($filePath);
42+
}, 'max_age=120;s_maxage=60');
43+
44+
$this->assertNotNull($response->getMaxAge());
45+
}
46+
3647
public function testAddHeaders()
3748
{
3849
$response = (new Cache)->handle(new Request, function () {

0 commit comments

Comments
 (0)