Skip to content

Commit 1ebc898

Browse files
committed
Merge branch 'andybeak-abeak/allow_no_cache_header'
Closes #21
2 parents 5508132 + 707d00d commit 1ebc898

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/Cache.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
5656

5757
// Cache-Control header
5858
if (!$response->hasHeader('Cache-Control')) {
59-
$response = $response->withHeader('Cache-Control', sprintf(
60-
'%s, max-age=%s%s',
61-
$this->type,
62-
$this->maxAge,
63-
$this->mustRevalidate ? ', must-revalidate' : ''
64-
));
59+
if ($this->maxAge === 0) {
60+
$response = $response->withHeader('Cache-Control', sprintf(
61+
'%s, no-cache%s',
62+
$this->type,
63+
$this->mustRevalidate ? ', must-revalidate' : ''
64+
));
65+
} else {
66+
$response = $response->withHeader('Cache-Control', sprintf(
67+
'%s, max-age=%s%s',
68+
$this->type,
69+
$this->maxAge,
70+
$this->mustRevalidate ? ', must-revalidate' : ''
71+
));
72+
}
6573
}
6674

6775
// Last-Modified header and conditional GET check

tests/CacheTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function testCacheControlHeaderWithMustRevalidate()
5252
$this->assertEquals('private, max-age=86400, must-revalidate', $cacheControl);
5353
}
5454

55+
public function testCacheControlHeaderWithZeroMaxAge()
56+
{
57+
$cache = new Cache('private', 0, false);
58+
$req = $this->requestFactory();
59+
$res = new Response();
60+
$next = function (Request $req, Response $res) {
61+
return $res;
62+
};
63+
$res = $cache($req, $res, $next);
64+
65+
$cacheControl = $res->getHeaderLine('Cache-Control');
66+
67+
$this->assertEquals('private, no-cache', $cacheControl);
68+
}
69+
5570
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
5671
{
5772
$cache = new Cache('public', 86400);

0 commit comments

Comments
 (0)