From ec1b1188d482eb6b519c4340f5ce2c25ecd9f87a Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Mon, 11 Oct 2021 19:38:00 +0200 Subject: [PATCH] Do not let SetCacheHeaders override the eTag. --- src/Illuminate/Http/Middleware/SetCacheHeaders.php | 2 +- tests/Http/Middleware/CacheTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Middleware/SetCacheHeaders.php b/src/Illuminate/Http/Middleware/SetCacheHeaders.php index b42dc2f2f822..770a523ae249 100644 --- a/src/Illuminate/Http/Middleware/SetCacheHeaders.php +++ b/src/Illuminate/Http/Middleware/SetCacheHeaders.php @@ -30,7 +30,7 @@ public function handle($request, Closure $next, $options = []) } if (isset($options['etag']) && $options['etag'] === true) { - $options['etag'] = md5($response->getContent()); + $options['etag'] = $response->getEtag() ?? md5($response->getContent()); } if (isset($options['last_modified'])) { diff --git a/tests/Http/Middleware/CacheTest.php b/tests/Http/Middleware/CacheTest.php index 0f75ed79d3a9..20b6579fff86 100644 --- a/tests/Http/Middleware/CacheTest.php +++ b/tests/Http/Middleware/CacheTest.php @@ -63,6 +63,15 @@ public function testGenerateEtag() $this->assertSame('max-age=100, public, s-maxage=200', $response->headers->get('Cache-Control')); } + public function testDoesNotOverrideEtag() + { + $response = (new Cache)->handle(new Request, function () { + return (new Response('some content'))->setEtag('XYZ'); + }, 'etag'); + + $this->assertSame('"XYZ"', $response->getEtag()); + } + public function testIsNotModified() { $request = new Request;