You are here

function BootstrapPageCacheTestCase::testConditionalRequests in SimpleTest 7

Test support for requests containing If-Modified-Since and If-None-Match headers.

File

tests/bootstrap.test, line 105

Class

BootstrapPageCacheTestCase

Code

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

  // Fill the cache.
  $this
    ->drupalGet('');
  $this
    ->drupalHead('');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('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, t('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, t('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, t('Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'));
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . $last_modified,
  ));
  $this
    ->assertResponse(200, t('Conditional request without If-None-Match returned 200 OK.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  $this
    ->drupalGet('', array(), array(
    'If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) + 1),
    'If-None-Match: ' . $etag,
  ));
  $this
    ->assertResponse(200, t('Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.'));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('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, t('Conditional request returned 200 OK for authenticated user.'));
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), t('Absense of Page was not cached.'));
}