minor #22099 HttpCache: New test for revalidating responses with an expired TTL (mpdude)

This PR was squashed before being merged into the 2.7 branch (closes #22099).

Discussion
----------

HttpCache: New test for revalidating responses with an expired TTL

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

See #22035, in particular [this and the following comments](https://github.com/symfony/symfony/pull/22035#issuecomment-287572234).

Commits
-------

067ab52ba0 HttpCache: New test for revalidating responses with an expired TTL
This commit is contained in:
Fabien Potencier 2017-03-21 15:46:55 -07:00
commit f9b64a206b

View File

@ -849,6 +849,42 @@ class HttpCacheTest extends HttpCacheTestCase
$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());