Skip to content

Commit

Permalink
Merge branch 'andybeak-abeak/allow_no_cache_header'
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
akrabat committed Sep 20, 2017
2 parents 5508132 + 707d00d commit 1ebc898
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,

// Cache-Control header
if (!$response->hasHeader('Cache-Control')) {
$response = $response->withHeader('Cache-Control', sprintf(
'%s, max-age=%s%s',
$this->type,
$this->maxAge,
$this->mustRevalidate ? ', must-revalidate' : ''
));
if ($this->maxAge === 0) {
$response = $response->withHeader('Cache-Control', sprintf(
'%s, no-cache%s',
$this->type,
$this->mustRevalidate ? ', must-revalidate' : ''
));
} else {
$response = $response->withHeader('Cache-Control', sprintf(
'%s, max-age=%s%s',
$this->type,
$this->maxAge,
$this->mustRevalidate ? ', must-revalidate' : ''
));
}
}

// Last-Modified header and conditional GET check
Expand Down
15 changes: 15 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public function testCacheControlHeaderWithMustRevalidate()
$this->assertEquals('private, max-age=86400, must-revalidate', $cacheControl);
}

public function testCacheControlHeaderWithZeroMaxAge()
{
$cache = new Cache('private', 0, false);
$req = $this->requestFactory();
$res = new Response();
$next = function (Request $req, Response $res) {
return $res;
};
$res = $cache($req, $res, $next);

$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('private, no-cache', $cacheControl);
}

public function testCacheControlHeaderDoesNotOverrideExistingHeader()
{
$cache = new Cache('public', 86400);
Expand Down

0 comments on commit 1ebc898

Please sign in to comment.