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()
- 8.9.x 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 163
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\Tests\page_cache\FunctionalCode
public function testConditionalRequests() : void {
$this->enablePageCaching();
// 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->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$etag = $this->getSession()
->getResponseHeader('ETag');
$last_modified = $this->getSession()
->getResponseHeader('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' => '',
]);
// Verify the page is not printed twice when the cache is warm.
$this->assertSession()
->responseNotMatches('#<html.*<html#');
$this->assertSession()
->statusCodeEquals(200);
$this->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
// 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->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
// 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);
// Verify that absence of Page was not cached.
$this->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.