Skip to content

Commit

Permalink
minor #22099 HttpCache: New test for revalidating responses with an e…
Browse files Browse the repository at this point in the history
…xpired 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](symfony/symfony#22035 (comment)).

Commits
-------

067ab52 HttpCache: New test for revalidating responses with an expired TTL
  • Loading branch information
fabpot committed Mar 21, 2017
2 parents 16b87d8 + 3dd1515 commit b87682a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Tests/HttpCache/HttpCacheTest.php
Expand Up @@ -849,6 +849,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

0 comments on commit b87682a

Please sign in to comment.