public function HttpCacheTest::testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php, line 652

Class

HttpCacheTest

Namespace

Symfony\Component\HttpKernel\Tests\HttpCache

Code

public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation() {
  $time = \DateTime::createFromFormat('U', time());
  $this
    ->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
    $response->headers
      ->set('Cache-Control', 'public');
    $response->headers
      ->set('Last-Modified', $time
      ->format(DATE_RFC2822));
    if ($time
      ->format(DATE_RFC2822) == $request->headers
      ->get('IF_MODIFIED_SINCE')) {
      $response
        ->setStatusCode(304);
      $response
        ->setContent('');
    }
  });

  // build initial request
  $this
    ->request('GET', '/');
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());
  $this
    ->assertNotNull($this->response->headers
    ->get('Last-Modified'));
  $this
    ->assertNotNull($this->response->headers
    ->get('X-Content-Digest'));
  $this
    ->assertEquals('Hello World', $this->response
    ->getContent());
  $this
    ->assertTraceContains('miss');
  $this
    ->assertTraceContains('store');
  $this
    ->assertTraceNotContains('stale');

  // build subsequent request; should be found but miss due to freshness
  $this
    ->request('GET', '/');
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());
  $this
    ->assertNotNull($this->response->headers
    ->get('Last-Modified'));
  $this
    ->assertNotNull($this->response->headers
    ->get('X-Content-Digest'));
  $this
    ->assertTrue($this->response->headers
    ->get('Age') <= 1);
  $this
    ->assertEquals('Hello World', $this->response
    ->getContent());
  $this
    ->assertTraceContains('stale');
  $this
    ->assertTraceContains('valid');
  $this
    ->assertTraceContains('store');
  $this
    ->assertTraceNotContains('miss');
}