Skip to content

Commit f6a77a1

Browse files
bug #19397 [2.7] [HttpFoundation] HttpCache refresh stale responses containing an ETag (maennchen)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7] [HttpFoundation] HttpCache refresh stale responses containing an ETag | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? |no | BC breaks? |no | Deprecations? |no | Tests pass? | yes | Fixed tickets | #19390, #6746 | License | MIT | Doc PR | This PR is the replacement of #19391, which points at the wrong branch. Commits ------- 96df6b9 [HttpFoundation] HttpCache refresh stale responses containing an ETag
2 parents 544a073 + 96df6b9 commit f6a77a1

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)