You are here

function PageCacheTest::testConditionalRequests in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/page_cache/src/Tests/PageCacheTest.php \Drupal\page_cache\Tests\PageCacheTest::testConditionalRequests()

Tests support of requests with If-Modified-Since and If-None-Match headers.

File

core/modules/page_cache/src/Tests/PageCacheTest.php, line 185
Contains \Drupal\page_cache\Tests\PageCacheTest.

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\page_cache\Tests

Code

function testConditionalRequests() {
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();

  // Fill the cache.
  $this
    ->drupalGet('');

  // Verify the page is not printed twice when the cache is cold.
  $this
    ->assertNoPattern('#<html.*<html#');
  $this
    ->drupalHead('');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $etag = $this
    ->drupalGetHeader('ETag');
  $last_modified = $this
    ->drupalGetHeader('Last-Modified');
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(304, 'Conditional request returned 304 Not Modified.');
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)),
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)),
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
  ));

  // Verify the page is not printed twice when the cache is warm.
  $this
    ->assertNoPattern('#<html.*<html#');
  $this
    ->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1),
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Absence of Page was not cached.');
}