From 1ce7d86c8eaf56b984152e65422ac21082d41928 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 13:31:49 +0100 Subject: [PATCH 1/2] HttpCache: New test for revalidating responses with an expired TTL --- .../Tests/HttpCache/HttpCacheTest.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index 7751f6e0ccfdc..85c6754a93d71 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -799,6 +799,39 @@ public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInform $this->assertTraceNotContains('miss'); } + public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation() + { + $time = \DateTime::createFromFormat('U', time()); + + $this->setNextResponse(200, [], 'Hello World', function (Request $request, Response $response) use ($time) { + $response->setSharedMaxAge(10); + $response->headers->set('Last-Modified', $time->format(DATE_RFC2822)); + }); + + // prime the cache + $this->request('GET', '/'); + + // next request before s-maxage has expired: Serve from cache without hitting the backend + $this->request('GET', '/'); + $this->assertHttpKernelIsNotCalled(); + $this->assertEquals(200, $this->response->getStatusCode()); + $this->assertEquals('Hello World', $this->response->getContent()); + $this->assertTraceContains('fresh'); + + sleep(15); // expire the cache + + $this->setNextResponse(304, [], '', function (Request $request, Response $response) use ($time) { + $this->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE')); + }); + + $this->request('GET', '/'); + $this->assertHttpKernelIsCalled(); + $this->assertEquals(200, $this->response->getStatusCode()); + $this->assertEquals('Hello World', $this->response->getContent()); + $this->assertTraceContains('stale'); + $this->assertTraceContains('valid'); + } + public function testValidatesCachedResponsesUseSameHttpMethod() { $test = $this; From a6fa008fa6be38569b33b82b6ca5f9e36d5dbc76 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 13:34:51 +0100 Subject: [PATCH 2/2] fabbot.io --- .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index 85c6754a93d71..ac3d35e681d57 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -11,9 +11,9 @@ namespace Symfony\Component\HttpKernel\Tests\HttpCache; -use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpKernel\HttpKernelInterface; /** @@ -803,7 +803,7 @@ public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInform { $time = \DateTime::createFromFormat('U', time()); - $this->setNextResponse(200, [], 'Hello World', function (Request $request, Response $response) use ($time) { + $this->setNextResponse(200, array(), 'Hello World', function (Request $request, Response $response) use ($time) { $response->setSharedMaxAge(10); $response->headers->set('Last-Modified', $time->format(DATE_RFC2822)); }); @@ -820,7 +820,7 @@ public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInform sleep(15); // expire the cache - $this->setNextResponse(304, [], '', function (Request $request, Response $response) use ($time) { + $this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time) { $this->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE')); });