public function HttpCacheTest::testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch

File

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

Class

HttpCacheTest

Namespace

Symfony\Component\HttpKernel\Tests\HttpCache

Code

public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch() {
  $time = new \DateTime();
  $this
    ->setNextResponse(200, array(), '', function ($request, $response) use ($time) {
    $response
      ->setStatusCode(200);
    $response->headers
      ->set('ETag', '12345');
    $response->headers
      ->set('Last-Modified', $time
      ->format(DATE_RFC2822));
    $response->headers
      ->set('Content-Type', 'text/plain');
    $response
      ->setContent('Hello World');
  });

  // only ETag matches
  $t = \DateTime::createFromFormat('U', time() - 3600);
  $this
    ->request('GET', '/', array(
    'HTTP_IF_NONE_MATCH' => '12345',
    'HTTP_IF_MODIFIED_SINCE' => $t
      ->format(DATE_RFC2822),
  ));
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());

  // only Last-Modified matches
  $this
    ->request('GET', '/', array(
    'HTTP_IF_NONE_MATCH' => '1234',
    'HTTP_IF_MODIFIED_SINCE' => $time
      ->format(DATE_RFC2822),
  ));
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());

  // Both matches
  $this
    ->request('GET', '/', array(
    'HTTP_IF_NONE_MATCH' => '12345',
    'HTTP_IF_MODIFIED_SINCE' => $time
      ->format(DATE_RFC2822),
  ));
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(304, $this->response
    ->getStatusCode());
}