From 9f9c7f9d5d7ff591664d484b769de111ebd54047 Mon Sep 17 00:00:00 2001 From: Kevin Robatel Date: Sun, 6 Sep 2015 14:18:08 +0200 Subject: [PATCH] #31 handle `Expires` with `Cache-Control` --- src/Strategy/PrivateCacheStrategy.php | 8 ++++++++ tests/HeaderCacheControlTest.php | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Strategy/PrivateCacheStrategy.php b/src/Strategy/PrivateCacheStrategy.php index 02ebcdb7..9e3a0f06 100644 --- a/src/Strategy/PrivateCacheStrategy.php +++ b/src/Strategy/PrivateCacheStrategy.php @@ -106,6 +106,14 @@ protected function getCacheObjectForCacheControl(ResponseInterface $response, Ke return $entry->hasValidationInformation() ? $entry : null; } + if ($response->hasHeader("Expires") + && $expireAt = \DateTime::createFromFormat(\DateTime::RFC1123, $response->getHeaderLine("Expires"))) { + return new CacheEntry( + $response, + $expireAt + ); + } + foreach ($this->ageKey as $key) { if ($cacheControl->has($key)) { return new CacheEntry( diff --git a/tests/HeaderCacheControlTest.php b/tests/HeaderCacheControlTest.php index 2b3b5f29..fc424714 100644 --- a/tests/HeaderCacheControlTest.php +++ b/tests/HeaderCacheControlTest.php @@ -52,6 +52,12 @@ public function setUp() ->withAddedHeader("Cache-Control", "no-cache") ->withAddedHeader("Etag", "TheHash") ); + case '/2s-expires': + return new FulfilledPromise( + (new Response()) + ->withAddedHeader("Cache-Control", "public") + ->withAddedHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2)) + ); } throw new \InvalidArgumentException(); @@ -106,4 +112,17 @@ public function testNoCacheHeader() $this->assertEquals(CacheMiddleware::HEADER_CACHE_HIT, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); } + public function testWithExpires() + { + $this->client->get("http://test.com/2s-expires"); + + $response = $this->client->get("http://test.com/2s-expires"); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_HIT, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + + sleep(3); + + $response = $this->client->get("http://test.com/2s-expires"); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + } + }