Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96df6b9

Browse files
Jonatan Männchenmaennchen
Jonatan Männchen
authored andcommitted
[HttpFoundation] HttpCache refresh stale responses containing an ETag
1 parent 37cd583 commit 96df6b9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ protected function lock(Request $request, Response $entry)
600600
*/
601601
protected function store(Request $request, Response $response)
602602
{
603+
if (!$response->headers->has('Date')) {
604+
$response->setDate(\DateTime::createFromFormat('U', time()));
605+
}
603606
try {
604607
$this->store->write($request, $response);
605608

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,31 @@ public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch(
181181
$this->assertEquals(304, $this->response->getStatusCode());
182182
}
183183

184+
public function testIncrementsMaxAgeWhenNoDateIsSpecifiedEventWhenUsingETag()
185+
{
186+
$this->setNextResponse(
187+
200,
188+
array(
189+
'ETag' => '1234',
190+
'Cache-Control' => 'public, s-maxage=60',
191+
)
192+
);
193+
194+
$this->request('GET', '/');
195+
$this->assertHttpKernelIsCalled();
196+
$this->assertEquals(200, $this->response->getStatusCode());
197+
$this->assertTraceContains('miss');
198+
$this->assertTraceContains('store');
199+
200+
sleep(2);
201+
202+
$this->request('GET', '/');
203+
$this->assertHttpKernelIsNotCalled();
204+
$this->assertEquals(200, $this->response->getStatusCode());
205+
$this->assertTraceContains('fresh');
206+
$this->assertEquals(2, $this->response->headers->get('Age'));
207+
}
208+
184209
public function testValidatesPrivateResponsesCachedOnTheClient()
185210
{
186211
$this->setNextResponse(200, array(), '', function ($request, $response) {

0 commit comments

Comments
 (0)