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

Skip to content

HttpCache: New test for revalidating responses with an expired TTL #22099

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 4 commits into from
Closed
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
36 changes: 36 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,42 @@ public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
$this->assertTraceNotContains('miss');
}

public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation()
{
$time = \DateTime::createFromFormat('U', time());

$this->setNextResponse(200, array(), 'Hello World', function (Request $request, Response $response) use ($time) {
$response->setSharedMaxAge(10);
$response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
});

// prime the cache
$this->request('GET', '/');

// next request before s-maxage has expired: Serve from cache
// without hitting the backend
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('fresh');

sleep(15); // expire the cache

$that = $this;

$this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time, $that) {
$that->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE'));
});

$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('stale');
$this->assertTraceContains('valid');
}

public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
{
$time = \DateTime::createFromFormat('U', time());
Expand Down