You are here

public function PageCacheTest::testHead in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testHead()

Tests that HEAD requests are treated the same as GET requests.

File

core/modules/page_cache/tests/src/Functional/PageCacheTest.php, line 546

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testHead() {

  /** @var \GuzzleHttp\ClientInterface $client */
  $client = $this
    ->getSession()
    ->getDriver()
    ->getClient()
    ->getClient();

  // GET, then HEAD.
  $url_a = $this
    ->buildUrl('system-test/set-header', [
    'query' => [
      'name' => 'Foo',
      'value' => 'bar',
    ],
  ]);
  $response_body = $this
    ->drupalGet($url_a);
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  $this
    ->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">bar</em>', $response_body);
  $response = $client
    ->request('HEAD', $url_a);
  $this
    ->assertEqual($response
    ->getHeaderLine('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertEqual($response
    ->getHeaderLine('Foo'), 'bar', 'Custom header was sent.');
  $this
    ->assertEqual('', $response
    ->getBody()
    ->getContents());

  // HEAD, then GET.
  $url_b = $this
    ->buildUrl('system-test/set-header', [
    'query' => [
      'name' => 'Foo',
      'value' => 'baz',
    ],
  ]);
  $response = $client
    ->request('HEAD', $url_b);
  $this
    ->assertEqual($response
    ->getHeaderLine('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->assertEqual($response
    ->getHeaderLine('Foo'), 'baz', 'Custom header was sent.');
  $this
    ->assertEqual('', $response
    ->getBody()
    ->getContents());
  $response_body = $this
    ->drupalGet($url_b);
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.');
  $this
    ->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">baz</em>', $response_body);
}