You are here

public function PageCacheTest::testConditionalRequests in Drupal 9

Same name and namespace in other branches
  1. 8 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 195

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\Tests\page_cache\Functional

Code

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
    ->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' => NULL,
  ]);

  // 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');
}