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

Skip to content

[2.8] [HttpFoundation] HttpCache refresh stale responses containing an ETag #19391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ protected function lock(Request $request, Response $entry)
*/
protected function store(Request $request, Response $response)
{
if (!$response->headers->has('Date')) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to explicitly call setDate to clarify what the code is doing even though one line containing the following would have the same effect:

$response->getDate();

$response->setDate(\DateTime::createFromFormat('U', time()));
}
try {
$this->store->write($request, $response);

Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch(
$this->assertEquals(304, $this->response->getStatusCode());
}

public function testIncrementsMaxAgeWhenNoDateIsSpecifiedEventWhenUsingETag()
{
$this->setNextResponse(
200,
array(
'ETag' => '1234',
'Cache-Control' => 'public, s-maxage=60',
)
);

$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('miss');
$this->assertTraceContains('store');

sleep(2);

$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('fresh');
$this->assertEquals(2, $this->response->headers->get('Age'));
}

public function testValidatesPrivateResponsesCachedOnTheClient()
{
$this->setNextResponse(200, array(), '', function ($request, $response) {
Expand Down