You are here

function BootstrapPageCacheTestCase::testPageCache in SimpleTest 7

Test cache headers.

File

tests/bootstrap.test, line 143

Class

BootstrapPageCacheTestCase

Code

function testPageCache() {
  variable_set('cache', CACHE_NORMAL);

  // Fill the cache.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Page was not cached.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));

  // Check cache.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary: Cookie header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));

  // Check replacing default headers.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Expires',
      'value' => 'Fri, 19 Nov 2008 05:00:00 GMT',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', t('Default header was replaced.'));
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Vary',
      'value' => 'User-Agent',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', t('Default header was replaced.'));

  // Check that authenticated users bypass the cache.
  $user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), t('Caching was bypassed.'));
  $this
    ->assertTrue(strpos($this
    ->drupalGetHeader('Vary'), 'Cookie') === FALSE, t('Vary: Cookie header was not sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', t('Cache-Control header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
}