responseHeaderDoesNotExist example

// Reset the password by username via the password reset page.     $this->drupalGet('user/password');
    $edit = ['name' => $this->account->getAccountName()];
    $this->submitForm($edit, 'Submit');
    $this->assertValidPasswordReset($edit['name']);

    $resetURL = $this->getResetURL();
    $this->drupalGet($resetURL);
    // Ensure that the current URL does not contain the hash and timestamp.     $this->assertSession()->addressEquals(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()]));

    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');

    // Ensure the password reset URL is not cached.     $this->drupalGet($resetURL);
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');

    // Check the one-time login page.     $this->assertSession()->pageTextContains($this->account->getAccountName());
    $this->assertSession()->pageTextContains('This login can be used only once.');
    $this->assertSession()->titleEquals('Reset password | Drupal');

    // Check successful login.
$config->set('cache.page.max_age', 300);
    $config->save();

    $account = $this->drupalCreateUser();
    $url = Url::fromRoute('router_test.11');

    // Ensure we can log in with valid authentication details.     $this->basicAuthGet($url$account->getAccountName()$account->pass_raw);
    $this->assertSession()->pageTextContains($account->getAccountName());
    $this->assertSession()->statusCodeEquals(200);
    $this->mink->resetSessions();
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
    // Check that Cache-Control is not set to public.     $this->assertSession()->responseHeaderNotContains('Cache-Control', 'public');

    // Ensure that invalid authentication details give access denied.     $this->basicAuthGet($url$account->getAccountName()$this->randomMachineName());
    $this->assertSession()->pageTextNotContains($account->getAccountName());
    $this->assertSession()->statusCodeEquals(403);
    $this->mink->resetSessions();

    // Ensure that the user is prompted to authenticate if they are not yet     // authenticated and the route only allows basic auth.
$this->expectExceptionMessage("Failed asserting that the response has a 'does-not-exist' header.");
    $this->assertSession()->responseHeaderExists('does-not-exist');
  }

  /** * Tests WebAssert::responseHeaderDoesNotExist(). * * @covers ::responseHeaderDoesNotExist */
  public function testResponseHeaderDoesNotExist() {
    $this->drupalGet('test-null-header');
    $this->assertSession()->responseHeaderDoesNotExist('does-not-exist');

    $this->expectException(AssertionFailedError::class);
    $this->expectExceptionMessage("Failed asserting that the response does not have a 'Null-Header' header.");
    $this->assertSession()->responseHeaderDoesNotExist('Null-Header');
  }

  /** * @covers ::pageTextMatchesCount */
  public function testPageTextMatchesCount() {
    $this->drupalLogin($this->drupalCreateUser());

    
// 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     // raw body returned.
$this->assertSession()->responseContains($error_details);
    $this->drupalGet('error-test/trigger-renderer-exception');
    $this->assertSession()->statusCodeEquals(500);
    $this->assertErrorMessage($error_renderer_exception);

    // Disable error reporting, ensure that 5xx responses are not cached.     $this->config('system.logging')
      ->set('error_level', ERROR_REPORTING_HIDE)
      ->save();

    $this->drupalGet('error-test/trigger-exception');
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()->responseHeaderNotContains('Cache-Control', 'public');
    $this->assertSession()->statusCodeEquals(500);
    $this->assertNoErrorMessage($error_exception);
  }

  /** * Helper function: assert that the error message is found. * * @internal */
  public function assertErrorMessage(array $error): void {
    
// Check correct headers.     $this->drupalGet('/en/system-test/echo/language test', []['Accept-Language' => 'en']);
    $this->assertSession()->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    $this->drupalGet('/fr/system-test/echo/language test', []['Accept-Language' => 'en']);
    $this->assertSession()->responseHeaderEquals('Content-Language', 'fr');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    $this->drupalGet('/system-test/echo/language test', []['Accept-Language' => 'en']);
    $this->assertSession()->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');

    // Check with UK browser.     $this->drupalGet('/system-test/echo/language test', []['Accept-Language' => 'en-UK,en']);
    $this->assertSession()->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');

    // Check with french browser.     $this->drupalGet('/system-test/echo/language test', []['Accept-Language' => 'fr-FR,fr']);
    $this->assertSession()->responseHeaderEquals('Content-Language', 'fr');
    $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');

    
'If-None-Match' => $etag,
    ]);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');

    // Ensure a conditional request by an authenticated user returns 200 OK.     $user = $this->drupalCreateUser();
    $this->drupalLogin($user);
    $this->drupalGet('', []['If-Modified-Since' => $last_modified, 'If-None-Match' => $etag]);
    $this->assertSession()->statusCodeEquals(200);
    // Verify that absence of Page was not cached.     $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
  }

  /** * Tests cache headers. */
  public function testPageCache() {
    $config = $this->config('system.performance');
    $config->set('cache.page.max_age', 300);
    $config->save();

    // Fill the cache.
$file->save();

    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());

    // Set file_test access header to allow the download.     file_test_reset();
    file_test_set_return('download', ['x-foo' => 'Bar']);
    $this->drupalGet($url);
    // Verify that header is set by file_test module on private download.     $this->assertSession()->responseHeaderEquals('x-foo', 'Bar');
    // Verify that page cache is disabled on private file download.     $this->assertSession()->responseHeaderDoesNotExist('x-drupal-cache');
    $this->assertSession()->statusCodeEquals(200);
    // Ensure hook_file_download is fired correctly.     $this->assertEquals($file->getFileUri(), \Drupal::state()->get('file_test.results')['download'][0][0]);

    // Test that the file transferred correctly.     $this->assertSame($contents$this->getSession()->getPage()->getContent(), 'Contents of the file are correct.');
    $http_client = $this->getHttpClient();

    // Try non-existent file.     file_test_reset();
    $not_found_url = $this->fileUrlGenerator->generateAbsoluteString('private://' . $this->randomMachineName() . '.txt');
    
$this->drupalGet('test-pipe-char');
    $this->assertSession()->linkNotExistsExact('foo|bar');
  }

  /** * Tests responseHeaderDoesNotExist() functionality. * * @see \Drupal\Tests\WebAssert::responseHeaderDoesNotExist() */
  public function testResponseHeaderDoesNotExist() {
    $this->drupalGet('test-pipe-char');
    $this->assertSession()->responseHeaderDoesNotExist('Foo-Bar');
  }

  /** * Tests linkNotExistsExact() functionality fail. * * @see \Drupal\Tests\WebAssert::linkNotExistsExact() */
  public function testInvalidLinkNotExistsExact() {
    $this->drupalGet('test-pipe-char');
    $this->expectException(ExpectationException::class);
    $this->expectExceptionMessage('Link with label foo|bar|baz found');
    
    \Drupal::service('module_installer')->uninstall(['page_cache']);
  }

  /** * Tests that Dynamic Page Cache works correctly, and verifies the edge cases. */
  public function testDynamicPageCache() {
    // Controllers returning plain response objects are ignored by Dynamic Page     // Cache.     $url = Url::fromUri('route:dynamic_page_cache_test.response');
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);

    // Controllers returning CacheableResponseInterface (cacheable response)     // objects are handled by Dynamic Page Cache.     $url = Url::fromUri('route:dynamic_page_cache_test.cacheable_response');
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');

    // Controllers returning render arrays, rendered as HTML responses, are     // handled by Dynamic Page Cache.
// 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/');

    // Verify that session was destroyed.     $this->drupalGet('');
    $this->assertSessionCookie(FALSE);
    // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages.     // $this->assertSessionEmpty(TRUE);     $this->assertSession()->pageTextNotContains('This is a dummy message.');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    
Home | Imprint | This part of the site doesn't use cookies.