You are here

public function PageCacheTest::testHead in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testHead()
  2. 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 510

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
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
  $this
    ->assertSession()
    ->responseHeaderEquals('Foo', 'bar');
  $this
    ->assertEquals('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">bar</em>', $response_body);
  $response = $client
    ->request('HEAD', $url_a);
  $this
    ->assertEquals('HIT', $response
    ->getHeaderLine('X-Drupal-Cache'), 'Page was cached.');
  $this
    ->assertEquals('bar', $response
    ->getHeaderLine('Foo'), 'Custom header was sent.');
  $this
    ->assertEquals('', $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
    ->assertEquals('MISS', $response
    ->getHeaderLine('X-Drupal-Cache'), 'Page was not cached.');
  $this
    ->assertEquals('baz', $response
    ->getHeaderLine('Foo'), 'Custom header was sent.');
  $this
    ->assertEquals('', $response
    ->getBody()
    ->getContents());
  $response_body = $this
    ->drupalGet($url_b);
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  $this
    ->assertSession()
    ->responseHeaderEquals('Foo', 'baz');
  $this
    ->assertEquals('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">baz</em>', $response_body);
}