8000 Keep s-maxage when expiry and validation are used in combination by mpdude · Pull Request #23130 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();

7FF3 $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