Fix that no-cache requires positive validation with the origin, even for fresh responses

This commit is contained in:
Matthias Pigulla 2020-01-10 22:32:00 +00:00
parent d1e31a4fe0
commit c8bdcb3408
2 changed files with 20 additions and 0 deletions

View File

@ -323,6 +323,10 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
return $this->validate($request, $entry, $catch);
}
if ($entry->headers->hasCacheControlDirective('no-cache')) {
return $this->validate($request, $entry, $catch);
}
$this->record($request, 'fresh');
$entry->headers->set('Age', $entry->getAge());

View File

@ -443,6 +443,22 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertTrue($this->response->headers->has('Age'));
}
public function testRevalidatesResponsesWithNoCacheDirectiveEvenIfFresh()
{
$this->setNextResponse(200, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag'], 'OK');
$this->request('GET', '/'); // warm the cache
sleep(5);
$this->setNextResponse(304, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag']);
$this->request('GET', '/');
$this->assertHttpKernelIsCalled(); // no-cache -> MUST have revalidated at origin
$this->assertTraceContains('valid');
$this->assertEquals('OK', $this->response->getContent());
$this->assertEquals(0, $this->response->getAge());
}
public function testCachesResponsesWithAnExpirationHeader()
{
$time = \DateTime::createFromFormat('U', time() + 5);