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;