From 1a8a8afdc6bb8ef0d9d0a6c67aceb7459c1bb99f Mon Sep 17 00:00:00 2001 From: Titouan Galopin Date: Wed, 17 Aug 2016 17:05:41 +0200 Subject: [PATCH] [HttpKernel] Fix HttpCache validation HTTP method --- .../Component/HttpKernel/HttpCache/HttpCache.php | 8 ++++++-- .../HttpKernel/Tests/HttpCache/HttpCacheTest.php | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php index f794d94e29..dcbce22b89 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php @@ -374,7 +374,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface $subRequest = clone $request; // send no head requests because we want content - $subRequest->setMethod('GET'); + if ('HEAD' === $request->getMethod()) { + $subRequest->setMethod('GET'); + } // add our cached last-modified validator $subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified')); @@ -435,7 +437,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface $subRequest = clone $request; // send no head requests because we want content - $subRequest->setMethod('GET'); + if ('HEAD' === $request->getMethod()) { + $subRequest->setMethod('GET'); + } // avoid that the backend sends no content $subRequest->headers->remove('if_modified_since'); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index b6164dd678..497f6fbde7 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -774,6 +774,21 @@ class HttpCacheTest extends HttpCacheTestCase $this->assertTraceNotContains('miss'); } + public function testValidatesCachedResponsesUseSameHttpMethod() + { + $test = $this; + + $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($test) { + $test->assertSame('OPTIONS', $request->getMethod()); + }); + + // build initial request + $this->request('OPTIONS', '/'); + + // build subsequent request + $this->request('OPTIONS', '/'); + } + public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation() { $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {