getResponseHeader example

$this->drupalLogin($admin_user);

    $edit = [
      'translation[config_names][views.view.test_view][display][default][display_options][title]' => 'Titel NL',
    ];
    $this->drupalGet('admin/structure/views/view/test_view/translate/nl/edit');
    $this->submitForm($edit, 'Save translation');
    $this->drupalLogout();

    $this->drupalGet('test-view');
    $this->assertSession()->titleEquals('Title EN | Drupal');
    $this->assertEquals('MISS', $this->getSession()->getResponseHeader('X-Drupal-Cache'));

    // Make sure the use of a title callback does not prevent caching of the     // View page.     $this->drupalGet('test-view');
    $this->assertEquals('HIT', $this->getSession()->getResponseHeader('X-Drupal-Cache'));

    // Test the breadcrumb on a deeper page because by default the breadcrumb     // doesn't render the current page title. It doesn't matter for the     // breadcrumb that the requested page does not exist.     $this->drupalGet('test-view/not-relevant');
    $this->assertSession()->linkExists('Title EN');
    
sort($expected_cache_contexts);

    // Confirm that the router can get to a controller.     $this->drupalGet('router_test/test1');
    $this->assertSession()->pageTextContains(TestControllers::LONG_TEXT);
    $session = $this->getSession();

    // Check expected headers from FinishResponseSubscriber.     $this->assertSession()->responseHeaderEquals('Content-language', 'en');
    $this->assertSession()->responseHeaderEquals('X-Content-Type-Options', 'nosniff');
    $this->assertSession()->responseHeaderEquals('X-Frame-Options', 'SAMEORIGIN');
    if (strcasecmp($session->getResponseHeader('vary'), 'accept-encoding') !== 0) {
      $this->assertSession()->responseHeaderDoesNotExist('Vary');
    }

    $this->drupalGet('router_test/test2');
    $this->assertSession()->pageTextContains('test2');
    // Check expected headers from FinishResponseSubscriber.     $headers = $session->getResponseHeaders();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous http_response rendered');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Max-Age', '-1 (Permanent)');
    // Confirm that the page wrapping is being added, so we're not getting a

  protected function verifyPageCacheContainsTags(Url $url$hit_or_miss$tags = FALSE) {
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', $hit_or_miss);

    if ($hit_or_miss === 'HIT' && is_array($tags)) {
      $cache_tags = explode(' ', $this->getSession()->getResponseHeader('X-Drupal-Cache-Tags'));
      $tags = array_unique($tags);
      $this->assertEmpty(array_diff($tags$cache_tags), 'Page cache tags contains all expected tags.');
    }
  }

}
$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,
    ]);
$config->set('cache.page.max_age', 300);
    $config->save();
    $this->drupalGet('');
    $this->assertSessionCookie(FALSE);
    // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages.     // $this->assertSessionEmpty(TRUE);     $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    // Start a new session by setting a message.     $this->drupalGet('session-test/set-message');
    $this->assertSessionCookie(TRUE);
    $this->assertNotNull($this->getSession()->getResponseHeader('Set-Cookie'));

    // Display the message, during the same request the session is destroyed     // and the session cookie is unset.     $this->drupalGet('');
    $this->assertSessionCookie(FALSE);
    $this->assertSessionEmpty(FALSE);
    // Verify that caching was bypassed.     $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()->pageTextContains('This is a dummy message.');
    // Verify that session cookie was deleted.     $this->assertSession()->responseHeaderMatches('Set-Cookie', '/SESS\w+=deleted/');

    
/** * Asserts expected BigPipe no-JS placeholders are present and replaced. * * @param array $expected_big_pipe_nojs_placeholders * Keys: BigPipe no-JS placeholder markup. Values: expected replacement * markup. * * @internal */
  protected function assertBigPipeNoJsPlaceholders(array $expected_big_pipe_nojs_placeholders): void {
    $this->assertSetsEqual(array_keys($expected_big_pipe_nojs_placeholders)array_map('rawurldecode', explode(' ', $this->getSession()->getResponseHeader('BigPipe-Test-No-Js-Placeholders'))));
    foreach ($expected_big_pipe_nojs_placeholders as $big_pipe_nojs_placeholder => $expected_replacement) {
      // Checking whether the replacement for the BigPipe no-JS placeholder       // $big_pipe_nojs_placeholder is present.       $this->assertSession()->responseNotContains($big_pipe_nojs_placeholder);
      if ($expected_replacement !== NULL) {
        $this->assertSession()->responseContains($expected_replacement);
      }
    }
  }

  /** * Asserts expected BigPipe placeholders are present and replaced. * * @param array $expected_big_pipe_placeholders * Keys: BigPipe placeholder IDs. Values: expected AJAX response. * @param array $expected_big_pipe_placeholder_stream_order * Keys: BigPipe placeholder IDs. Values: expected AJAX response. Keys are * defined in the order that they are expected to be rendered & streamed. * * @internal */
    $config = $this->config('language.negotiation');
    $config->set('url.prefixes', ['en' => '', 'fr' => 'fr', 'zh-hant' => 'zh'])->save();
    $this->rebuildContainer();

    $this->account->preferred_langcode = $setPreferredLangcode;
    $this->account->save();
    $this->assertSame($setPreferredLangcode$this->account->getPreferredLangcode(FALSE));

    // Test Default langcode is different from active langcode when visiting different.     if ($setPreferredLangcode !== 'en') {
      $this->drupalGet($prefix . '/user/password');
      $this->assertSame($activeLangcode$this->getSession()->getResponseHeader('Content-language'));
      $this->assertSame('en', $this->languageManager->getDefaultLanguage()->getId());
    }

    // Test password reset with language prefixes.     $this->drupalGet($visitingUrl);
    $edit = ['name' => $this->account->getAccountName()];
    $this->submitForm($edit, 'Submit');
    $this->assertValidPasswordReset($edit['name']);

    $resetURL = $this->getResetURL();
    $this->assertStringContainsString($expectedResetUrl$resetURL);
    
$collection_output = Json::decode($this->drupalGet('/jsonapi/node/article', [
      'query' => ['filter' => $filter] + $default_sort,
    ]));
    $this->assertSession()->statusCodeEquals(200);
    $this->assertCount(0, $collection_output['data']);

    // Request in maintenance mode returns valid JSON.     $this->container->get('state')->set('system.maintenance_mode', TRUE);
    $response = $this->drupalGet('/jsonapi/taxonomy_term/tags');
    $this->assertSession()->statusCodeEquals(503);
    $this->assertSession()->responseHeaderContains('Content-Type', 'application/vnd.api+json');
    $retry_after_time = $this->getSession()->getResponseHeader('Retry-After');
    $this->assertTrue($retry_after_time >= 5 && $retry_after_time <= 10);
    $expected_message = 'Drupal is currently under maintenance. We should be back shortly. Thank you for your patience.';
    $this->assertSame($expected_message, Json::decode($response)['errors'][0]['detail']);

    // Test that logged in user does not get logged out in maintenance mode     // when hitting jsonapi route.     $this->container->get('state')->set('system.maintenance_mode', FALSE);
    $this->drupalLogin($this->userCanViewProfiles);
    $this->container->get('state')->set('system.maintenance_mode', TRUE);
    $this->drupalGet('/jsonapi/taxonomy_term/tags');
    $this->assertSession()->statusCodeEquals(503);
    
// Verify a cache hit, but also the presence of the correct cache tags.     $expected_tags = Cache::mergeTags($referencing_entity_cache_tags$page_cache_tags);
    $expected_tags = Cache::mergeTags($expected_tags$page_cache_tags_referencing_entity);
    $this->verifyPageCache($listing_url, 'HIT', $expected_tags);

    // Prime the page cache for the empty listing.     $this->verifyPageCache($empty_entity_listing_url, 'MISS');
    // Verify a cache hit, but also the presence of the correct cache tags.     $this->verifyPageCache($empty_entity_listing_url, 'HIT', $empty_entity_listing_cache_tags);
    // Verify the entity type's list cache contexts are present.     $contexts_in_header = $this->getSession()->getResponseHeader('X-Drupal-Cache-Contexts');
    $this->assertEqualsCanonicalizing(Cache::mergeContexts($page_cache_contexts$this->getAdditionalCacheContextsForEntityListing())empty($contexts_in_header) ? [] : explode(' ', $contexts_in_header));

    // Prime the page cache for the listing containing the referenced entity.     $this->verifyPageCache($nonempty_entity_listing_url, 'MISS', $nonempty_entity_listing_cache_tags);
    // Verify a cache hit, but also the presence of the correct cache tags.     $this->verifyPageCache($nonempty_entity_listing_url, 'HIT', $nonempty_entity_listing_cache_tags);
    // Verify the entity type's list cache contexts are present.     $contexts_in_header = $this->getSession()->getResponseHeader('X-Drupal-Cache-Contexts');
    $this->assertEqualsCanonicalizing(Cache::mergeContexts($page_cache_contexts$this->getAdditionalCacheContextsForEntityListing())empty($contexts_in_header) ? [] : explode(' ', $contexts_in_header));

    // Verify that after modifying the referenced entity, there is a cache miss
/** * Gets a specific header value as array. * * @param string $header_name * The header name. * * @return string[] * The header value, potentially exploded by spaces. */
  protected function getCacheHeaderValues($header_name) {
    $header_value = $this->getSession()->getResponseHeader($header_name);
    return empty($header_value) ? [] : explode(' ', $header_value);
  }

  /** * Asserts whether an expected cache context was present in the last response. * * @param string $expected_cache_context * The expected cache context. */
  protected function assertCacheContext($expected_cache_context) {
    $cache_contexts = explode(' ', $this->getSession()->getResponseHeader('X-Drupal-Cache-Contexts'));
    
Home | Imprint | This part of the site doesn't use cookies.