function PageCacheTest::testConditionalRequests
Same name in other branches
- 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testConditionalRequests()
- 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testConditionalRequests()
- 11.x 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.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.