diff --git a/src/Cache.php b/src/Cache.php index 287007f..c488549 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -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 diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 28cc2e7..110f732 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -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);