From b909acf2eecc36624cd1277c1aa863bf4cb01b52 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Fri, 4 Mar 2022 07:24:56 +0100 Subject: [PATCH] [HttpFoundation] Fix PHP 8.1 deprecation in isNotModified --- src/Symfony/Component/HttpFoundation/Response.php | 3 +-- .../HttpFoundation/Tests/ResponseTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index bd990e9c04ae7..bfdc1b24a789a 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -1079,8 +1079,7 @@ public function isNotModified(Request $request): bool $lastModified = $this->headers->get('Last-Modified'); $modifiedSince = $request->headers->get('If-Modified-Since'); - if ($ifNoneMatchEtags = $request->getETags()) { - $etag = $this->getEtag(); + if (($ifNoneMatchEtags = $request->getETags()) && (null !== $etag = $this->getEtag())) { if (0 == strncmp($etag, 'W/', 2)) { $etag = substr($etag, 2); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 473406287dd09..734961b80e88b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -284,6 +284,20 @@ public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified() $this->assertFalse($response->isNotModified($request)); } + public function testIfNoneMatchWithoutETag() + { + $request = new Request(); + $request->headers->set('If-None-Match', 'randomly_generated_etag'); + + $this->assertFalse((new Response())->isNotModified($request)); + + // Test wildcard + $request = new Request(); + $request->headers->set('If-None-Match', '*'); + + $this->assertFalse((new Response())->isNotModified($request)); + } + public function testIsValidateable() { $response = new Response('', 200, ['Last-Modified' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);