function FinishResponseSubscriberTest::testDebugHeaders
Same name and namespace in other branches
- main core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest::testDebugHeaders()
Finish subscriber outputs tags, context, max-age if debug is on.
File
-
core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ FinishResponseSubscriberTest.php, line 157
Class
Namespace
Drupal\Tests\Core\EventSubscriberCode
public function testDebugHeaders() : void {
$finishSubscriber = new FinishResponseSubscriber($this->languageManager, $this->getConfigFactoryStub(), $this->requestPolicy, $this->responsePolicy, $this->cacheContextsManager, $this->time, TRUE);
$this->languageManager
->method('getCurrentLanguage')
->willReturn(new Language([
'id' => 'en',
]));
$this->cacheContextsManager
->method('optimizeTokens')
->willReturn([
'context1',
'context2',
]);
$request = $this->createStub(Request::class);
$response = $this->createStub(CacheableResponse::class);
$response->headers = new ResponseHeaderBag();
// Set cache tags, context, max-age.
$cacheData = (new CacheableMetadata())->setCacheTags([
'tag1',
'tag2',
])
->setCacheContexts([
'context1',
'context2',
])
->setCacheMaxAge(123);
$response->method('getCacheableMetadata')
->willReturn($cacheData);
$event = new ResponseEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
$finishSubscriber->onRespond($event);
// Check that X-Drupal-Cache-Tags is in the response header.
$this->assertSame([
'tag1 tag2',
], $response->headers
->all('X-Drupal-Cache-Tags'));
$this->assertSame([
'context1 context2',
], $response->headers
->all('X-Drupal-Cache-Contexts'));
$this->assertSame([
'123',
], $response->headers
->all('X-Drupal-Cache-Max-Age'));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.