You are here

public function PageCacheTest::testNoUrlNormalization 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::testNoUrlNormalization()
  2. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testNoUrlNormalization()

Tests that URLs are cached in a not normalized form.

File

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

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testNoUrlNormalization() {

  // Use absolute URLs to avoid any processing.
  $url = Url::fromRoute('<front>')
    ->setAbsolute()
    ->toString();

  // In each test, the first array value is raw URL, the second one is the
  // possible normalized URL.
  $tests = [
    [
      $url . '?z=z&a=a',
      $url . '?a=a&z=z',
    ],
    [
      $url . '?a=b+c',
      $url . '?a=b%20c',
    ],
  ];
  foreach ($tests as [
    $url_raw,
    $url_normalized,
  ]) {

    // Initialize cache on raw URL.
    $headers = $this
      ->getHeaders($url_raw);
    $this
      ->assertEquals('MISS', $headers['X-Drupal-Cache']);

    // Ensure cache was set.
    $headers = $this
      ->getHeaders($url_raw);
    $this
      ->assertEquals('HIT', $headers['X-Drupal-Cache'], "Cache was set for {$url_raw} URL.");

    // Check if the normalized URL is not cached.
    $headers = $this
      ->getHeaders($url_normalized);
    $this
      ->assertEquals('MISS', $headers['X-Drupal-Cache'], "Cache is missing for {$url_normalized} URL.");
  }
}