1 bootstrap.test BootstrapPageCacheTestCase::testConditionalRequests()

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

File

core/modules/simpletest/tests/bootstrap.test, line 117

Class

BootstrapPageCacheTestCase

Code

function testConditionalRequests() {
  config_set('system.core', 'cache', 1);
  config_set('system.core', 'page_cache_background_fetch', 0);
  config_set('system.core', 'page_cache_maximum_age', 300);

  // Fill the cache.
  $test_path = 'system-test/hello-world';
  $this->backdropGet($test_path);

  sleep(5);
  $this->backdropHead($test_path);
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
  $etag = $this->backdropGetHeader('ETag');
  $last_modified = $this->backdropGetHeader('Last-Modified');

  // Match a cache based on Etag only.
  $this->backdropGet($test_path, array(), array('If-None-Match: ' . $etag));
  $this->assertResponse(304, 'Conditional request with Etag only returned 304 Not Modified.');

  // Include both Etag and Date, even though the date is ignored.
  // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
  $this->backdropGet($test_path, array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  $this->assertResponse(304, 'Conditional request with Etag and unnecessary If-Modified-Since returned 304 Not Modified.');

  // Match a cache based on date only.
  $this->backdropGet($test_path, array(), array('If-Modified-Since: ' . $last_modified));
  $this->assertResponse(304, 'Conditional request without If-None-Match returned 304 Not Modified.');

  // Try a conditional request that is too stale to get a cached copy.
  $this->backdropGet($test_path, array(), array('If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) - 1)));
  $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date older than Last-Modified returned 200 OK.');
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');

  $user = $this->backdropCreateUser();
  $this->backdropLogin($user);
  $this->backdropGet($test_path, 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->backdropGetHeader('X-Backdrop-Cache'), 'Absence of Page was not cached.');
}