public function RouterTest::testFinishResponseSubscriber in Drupal 10
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Functional/Routing/RouterTest.php \Drupal\Tests\system\Functional\Routing\RouterTest::testFinishResponseSubscriber()
- 9 core/modules/system/tests/src/Functional/Routing/RouterTest.php \Drupal\Tests\system\Functional\Routing\RouterTest::testFinishResponseSubscriber()
Confirms that our FinishResponseSubscriber logic works properly.
File
- core/
modules/ system/ tests/ src/ Functional/ Routing/ RouterTest.php, line 35
Class
- RouterTest
- Functional class for the full integrated routing system.
Namespace
Drupal\Tests\system\Functional\RoutingCode
public function testFinishResponseSubscriber() {
$renderer_required_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
]);
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
// raw body returned.
$this
->assertSession()
->responseContains('</html>');
// In some instances, the subrequest handling may get confused and render
// a page inception style. This test verifies that is not happening.
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
// Confirm that route-level access check's cacheability is applied to the
// X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers.
// 1. controller result: render array, globally cacheable route access.
$this
->drupalGet('router_test/test18');
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
]);
sort($expected_cache_contexts);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Max-Age', '60');
// 2. controller result: render array, per-role cacheable route access.
$this
->drupalGet('router_test/test19');
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
'user.roles',
]);
sort($expected_cache_contexts);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
// 3. controller result: Response object, globally cacheable route access.
$this
->drupalGet('router_test/test1');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Contexts');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Tags');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Max-Age');
// 4. controller result: Response object, per-role cacheable route access.
$this
->drupalGet('router_test/test20');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Contexts');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Tags');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Max-Age');
// 5. controller result: CacheableResponse object, globally cacheable route access.
$this
->drupalGet('router_test/test21');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', '');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
// 6. controller result: CacheableResponse object, per-role cacheable route access.
$this
->drupalGet('router_test/test22');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', 'user.roles');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
// Finally, verify that the X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags
// headers are not sent when their container parameter is set to FALSE.
$this
->drupalGet('router_test/test18');
$this
->assertSession()
->responseHeaderExists('X-Drupal-Cache-Contexts');
$this
->assertSession()
->responseHeaderExists('X-Drupal-Cache-Tags');
$this
->assertSession()
->responseHeaderExists('X-Drupal-Cache-Max-Age');
$this
->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
$this
->rebuildContainer();
$this
->resetAll();
$this
->drupalGet('router_test/test18');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Contexts');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Tags');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache-Max-Age');
}