Skip to content

Commit

Permalink
Fix middleware "SetCacheHeaders" with file responses (#44063)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
clementbirkle authored Sep 9, 2022
1 parent 1b20ec4 commit cbeb50b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

0 comments on commit cbeb50b

Please sign in to comment.