responseHeaderEquals example


  public function testPageCacheTags() {
    $config = $this->config('system.performance');
    $config->set('cache.page.max_age', 300);
    $config->save();

    $path = 'system-test/cache_tags_page';
    $tags = ['system_test_cache_tags_page'];
    $this->drupalGet($path);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    // Verify a cache hit, but also the presence of the correct cache tags.     $this->drupalGet($path);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $cid_parts = [Url::fromRoute('system_test.cache_tags_page', []['absolute' => TRUE])->toString(), ''];
    $cid = implode(':', $cid_parts);
    $cache_entry = \Drupal::cache('page')->get($cid);
    sort($cache_entry->tags);
    $expected_tags = [
      'config:user.role.anonymous',
      'http_response',
      
$this->drupalGet('node/' . $nid);
    // No image style cache tag should be found.     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');

    $this->assertSession()->responseMatches('/<a(.*?)href="' . preg_quote($this->fileUrlGenerator->generateString($image_uri), '/') . '"(.*?)>\s*<picture/');
    // Verify that the image can be downloaded.     $this->assertEquals(file_get_contents($test_image->uri)$this->drupalGet($this->fileUrlGenerator->generateAbsoluteString($image_uri)), 'File was downloaded successfully.');
    if ($scheme == 'private') {
      // Only verify HTTP headers when using private scheme and the headers are       // sent by Drupal.       $this->assertSession()->responseHeaderEquals('Content-Type', 'image/png');
      $this->assertSession()->responseHeaderContains('Cache-Control', 'private');

      // Log out and ensure the file cannot be accessed.       $this->drupalLogout();
      $this->drupalGet($this->fileUrlGenerator->generateAbsoluteString($image_uri));
      $this->assertSession()->statusCodeEquals(403);

      // Log in again.       $this->drupalLogin($this->adminUser);
    }

    

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

}
$edit['pass'] = $user->passRaw;
    $this->drupalGet('admin/people/permissions');
    $this->submitForm($edit, 'Log in');
    $this->assertSession()->pageTextNotContains('User login');

    // Check that we are still on the same page.     $this->assertSession()->addressEquals(Url::fromRoute('user.admin_permissions'));

    // Now, log out and repeat with a non-403 page.     $this->drupalLogout();
    $this->drupalGet('filter/tips');
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
    $this->submitForm($edit, 'Log in');
    $this->assertSession()->pageTextNotContains('User login');
    // Verify that we are still on the same page after login for allowed page.     $this->assertSession()->responseMatches('!<title.*?Compose tips.*?</title>!');

    // Log out again and repeat with a non-403 page including query arguments.     $this->drupalLogout();
    $this->drupalGet('filter/tips', ['query' => ['foo' => 'bar']]);
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
    $this->submitForm($edit, 'Log in');
    $this->assertSession()->pageTextNotContains('User login');
    

  protected $defaultTheme = 'stark';

  /** * Tests rendering of ['#attached']. */
  public function testAttachments() {
    // Test ['#attached']['http_header] = ['Status', $code].     $this->drupalGet('/render_attached_test/teapot');
    $this->assertSession()->statusCodeEquals(418);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    // Repeat for the cache.     $this->drupalGet('/render_attached_test/teapot');
    $this->assertSession()->statusCodeEquals(418);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');

    // Test ['#attached']['http_header'] with various replacement rules.     $this->drupalGet('/render_attached_test/header');
    $this->assertTeapotHeaders();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    // Repeat for the cache.     $this->drupalGet('/render_attached_test/header');
    


  /** * Tests the cacheability of the table display. */
  public function testTableCacheability() {
    \Drupal::service('module_installer')->uninstall(['page_cache']);

    $url = 'test-table';
    $this->drupalGet($url);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
  }

}

  protected function setUp(): void {
    parent::setUp();

    Vocabulary::create(['vid' => 'test'])->save();
  }

  public function testSerialization() {
    $this->drupalGet('/vocabulary_serialization_test/test');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSame('this is the output', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    $this->drupalGet('/vocabulary_serialization_test/test');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSame('this is the output', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  }

}
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Asserts the cache context for the wrapper format is always present. */
  public function testWrapperFormatCacheContext() {
    $this->drupalGet('common-test/type-link-active-class');
    $this->assertStringStartsWith("<!DOCTYPE html>\n<html", $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->assertSession()->titleEquals('Test active link class | Drupal');
    $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);

    $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]);
    $this->assertSession()->responseHeaderEquals('Content-Type', 'application/json');
    $json = Json::decode($this->getSession()->getPage()->getContent());
    $this->assertEquals(['content', 'title']array_keys($json));
    $this->assertSame('Test active link class', $json['title']);
    $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
  }

}


  /** * Tests that SVGZ files are served with Content-Encoding: gzip. */
  public function testSvgzContentEncoding() {
    $this->drupalGet('core/modules/system/tests/logo.svgz');
    $this->assertSession()->statusCodeEquals(200);

    // Use x-encoded-content-encoding because of Content-Encoding responses     // (gzip, deflate, etc.) are automatically decoded by Guzzle.     $this->assertSession()->responseHeaderEquals('x-encoded-content-encoding', 'gzip');
  }

}
protected function getFormBuildId() {
    // Ensure the hidden 'form_build_id' field is unique.     $this->assertSession()->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
    return (string) $this->assertSession()->hiddenFieldExists('form_build_id')->getAttribute('value');
  }

  /** * Build-id is regenerated when validating cached form. */
  public function testValidateFormStorageOnCachedPage() {
    $this->drupalGet('form-test/form-storage-page-cache');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()->pageTextContains('No old build id');
    $build_id_initial = $this->getFormBuildId();

    // Trigger validation error by submitting an empty title.     $edit = ['title' => ''];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('No old build id');
    $build_id_first_validation = $this->getFormBuildId();
    $this->assertNotEquals($build_id_initial$build_id_first_validation, 'Build id changes when form validation fails');

    // Trigger validation error by again submitting an empty title.

  protected static $modules = ['system', 'default_format_test'];

  /** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  public function testFoo() {
    $this->drupalGet('/default_format_test/human');
    $this->assertSame('format:html', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->drupalGet('/default_format_test/human');
    $this->assertSame('format:html', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');

    $this->drupalGet('/default_format_test/machine');
    $this->assertSame('format:json', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->drupalGet('/default_format_test/machine');
    $this->assertSame('format:json', $this->getSession()->getPage()->getContent());
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  }

  
$generate_url_from_relative_path = $this->style->buildUrl($relative_path$clean_url);
    $this->assertEquals($generate_url$generate_url_from_relative_path);
    $this->config('system.file')->set('default_scheme', 'temporary')->save();

    // Fetch the URL that generates the file.     $this->drupalGet($generate_url);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertFileExists($generated_uri);
    // assertRaw can't be used with string containing non UTF-8 chars.     $this->assertNotEmpty(file_get_contents($generated_uri), 'URL returns expected file.');
    $image = $this->container->get('image.factory')->get($generated_uri);
    $this->assertSession()->responseHeaderEquals('Content-Type', $image->getMimeType());
    $this->assertSession()->responseHeaderEquals('Content-Length', (string) $image->getFileSize());

    // Check that we did not download the original file.     $original_image = $this->container->get('image.factory')
      ->get($original_uri);
    $this->assertSession()->responseHeaderNotEquals('Content-Length', (string) $original_image->getFileSize());

    if ($scheme == 'private') {
      $this->assertSession()->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
      // Check that Cache-Control header contains 'no-cache' to prevent caching.       $this->assertSession()->responseHeaderContains('Cache-Control', 'no-cache');
      
// Find the given content.     foreach ((array) $expected_content as $content) {
      $assert_session->pageTextContains($content);
    }
    if ($expected_cache_contexts) {
      $assert_session->responseHeaderContains('X-Drupal-Cache-Contexts', $expected_cache_contexts);
    }
    if ($expected_cache_tags) {
      $assert_session->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tags);
    }
    $assert_session->responseHeaderEquals('X-Drupal-Dynamic-Cache', $expected_dynamic_cache);
  }

  /** * Creates a node with a section field. * * @param array $section_values * An array of values for a section field. * * @return \Drupal\node\NodeInterface * The node object. */
  
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $file->getCacheTags()[0]);
    // @todo Remove in https://www.drupal.org/node/2646744.     $this->assertCacheContext('url.site');
    // Verify that no image style cache tags are found.     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'image_style:');
    $this->assertSession()->responseContains($default_output);
    // Verify that the image can be downloaded.     $this->assertEquals(file_get_contents($test_image->uri)$this->drupalGet($file->createFileUrl(FALSE)), 'File was downloaded successfully.');
    if ($scheme == 'private') {
      // Only verify HTTP headers when using private scheme and the headers are       // sent by Drupal.       $this->assertSession()->responseHeaderEquals('Content-Type', 'image/png');
      $this->assertSession()->responseHeaderContains('Cache-Control', 'private');

      // Log out and ensure the file cannot be accessed.       $this->drupalLogout();
      $this->drupalGet($file->createFileUrl(FALSE));
      $this->assertSession()->statusCodeEquals(403);

      // Log in again.       $this->drupalLogin($this->adminUser);
    }

    


  /** * Tests dynamic page cache. */
  public function testDynamicPageCache(): void {
    $this->drupalLogin($this->drupalCreateUser([
      'access toolbar',
      'access announcements',
    ]));
    // Front-page is visited right after login.     $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
    // Reload the page, it should be cached now.     $this->drupalGet(Url::fromRoute('<front>'));
    $this->assertSession()->elementExists('css', '[data-drupal-announce-trigger]');
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
  }

}
Home | Imprint | This part of the site doesn't use cookies.