Skip to content

Commit

Permalink
Embedding a response that combines expiration and validation, that sh…
Browse files Browse the repository at this point in the history
…ould not defeat expiration on the combined response
  • Loading branch information
mpdude committed Jun 14, 2017
1 parent 551e5ba commit 09bcbc7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -39,7 +39,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
*/
public function add(Response $response)
{
if ($response->isValidateable() || !$response->isCacheable()) {
if (!$response->isFresh() || !$response->isCacheable()) {
$this->cacheable = false;
} else {
$maxAge = $response->getMaxAge();
Expand Down Expand Up @@ -70,6 +70,9 @@ public function update(Response $response)
if ($response->isValidateable()) {
$response->setEtag(null);
$response->setLastModified(null);
}

if (!$response->isFresh()) {
$this->cacheable = false;
}

Expand Down
Expand Up @@ -178,4 +178,45 @@ public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
// Not sure if we should pass "max-age: 60" in this case, as long as the response is private and
// that's the more conservative of both the master and embedded response...?
}

public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
{
/* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
* and both the main and embedded response provide s-maxage, then the more restricting value of both
* should be fine, regardless of whether the embedded response can be validated later on or must be
* completely regenerated.
*/
$cacheStrategy = new ResponseCacheStrategy();

$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);

$embeddedResponse = new Response();
$embeddedResponse->setSharedMaxAge(60);
$embeddedResponse->setEtag('foo');

$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);

$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
}

public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
{
$cacheStrategy = new ResponseCacheStrategy();

$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$masterResponse->setEtag('foo');
$masterResponse->setLastModified(new \DateTime());

$embeddedResponse = new Response();
$embeddedResponse->setSharedMaxAge(60);

$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);

$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
$this->assertFalse($masterResponse->isValidateable());
}
}

0 comments on commit 09bcbc7

Please sign in to comment.