Skip to content

Commit

Permalink
#31 handle Expires with Cache-Control
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevinrob committed Sep 6, 2015
1 parent ea0523a commit 9f9c7f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Strategy/PrivateCacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions tests/HeaderCacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

}

0 comments on commit 9f9c7f9

Please sign in to comment.