From fd80c8f91d95c62ebb8d052546bf7d0b7ac74831 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 18:52:14 +0100 Subject: [PATCH 1/4] HttpCache: New test for revalidating responses with an expired TTL --- .../Tests/HttpCache/HttpCacheTest.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index b6164dd6782fb..827da4669d3ac 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -809,6 +809,40 @@ public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation() $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 testReplacesCachedResponsesWhenValidationResultsInNon304Response() { $time = \DateTime::createFromFormat('U', time()); From 814a293cc0ee215bd2f61bb8d416d8c335879d4f Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 18:54:20 +0100 Subject: [PATCH 2/4] fabbot.io --- .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index 827da4669d3ac..e0b88f984b6da 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -813,7 +813,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)); }); @@ -831,7 +831,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')); }); From fc743628fc018c1e62b31dfdcce2e05367f3fcae Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 23:21:11 +0100 Subject: [PATCH 3/4] PHP 5.3: Use $that instead of $this --- .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index e0b88f984b6da..d5faf5e9f5add 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -831,8 +831,10 @@ public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInform sleep(15); // expire the cache - $this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time) { - $this->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE')); + $that = $this; + + $this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time, $that) { + $that->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE')); }); $this->request('GET', '/'); From ef2e95fa66b1951915172def2babf1d3cb1e289a Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 21 Mar 2017 23:22:35 +0100 Subject: [PATCH 4/4] Fix CS --- .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index d5faf5e9f5add..985549d13df33 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -832,7 +832,7 @@ public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInform sleep(15); // expire the cache $that = $this; - + $this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time, $that) { $that->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE')); });