public function PageCacheTest::testConditionalRequests in Drupal 8
Same name and namespace in other branches
- 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testConditionalRequests()
Tests support of requests with If-Modified-Since and If-None-Match headers.
File
- core/
modules/ page_cache/ tests/ src/ Functional/ PageCacheTest.php, line 189
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\Tests\page_cache\FunctionalCode
public 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
->assertSession()
->responseNotMatches('#<html.*<html#');
$this
->drupalGet('');
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$etag = $this
->drupalGetHeader('ETag');
$last_modified = $this
->drupalGetHeader('Last-Modified');
// Ensure a conditional request returns 304 Not Modified.
$this
->drupalGet('', [], [
'If-Modified-Since' => $last_modified,
'If-None-Match' => $etag,
]);
$this
->assertSession()
->statusCodeEquals(304);
// Ensure a conditional request with obsolete If-Modified-Since date
// returns 304 Not Modified.
$this
->drupalGet('', [], [
'If-Modified-Since' => gmdate(DATE_RFC822, strtotime($last_modified)),
'If-None-Match' => $etag,
]);
$this
->assertSession()
->statusCodeEquals(304);
// Ensure a conditional request with obsolete If-Modified-Since date
// returns 304 Not Modified.
$this
->drupalGet('', [], [
'If-Modified-Since' => gmdate(DATE_RFC850, strtotime($last_modified)),
'If-None-Match' => $etag,
]);
$this
->assertSession()
->statusCodeEquals(304);
// Ensure a conditional request without If-None-Match returns 200 OK.
$this
->drupalGet('', [], [
'If-Modified-Since' => $last_modified,
'If-None-Match' => NULL,
]);
// Verify the page is not printed twice when the cache is warm.
$this
->assertSession()
->responseNotMatches('#<html.*<html#');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
// Ensure a conditional request with If-Modified-Since newer than
// Last-Modified returns 200 OK.
$this
->drupalGet('', [], [
'If-Modified-Since' => gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1),
'If-None-Match' => $etag,
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
// Ensure a conditional request by an authenticated user returns 200 OK.
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$this
->drupalGet('', [], [
'If-Modified-Since' => $last_modified,
'If-None-Match' => $etag,
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'), 'Absence of Page was not cached.');
}