function FinishResponseSubscriberTest::testExistingHeaders

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest::testExistingHeaders()

Finish subscriber should not overwrite existing header values.

@covers ::onRespond

File

core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php, line 116

Class

FinishResponseSubscriberTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21EventSubscriber%21FinishResponseSubscriber.php/class/FinishResponseSubscriber/11.x" title="Response subscriber to handle finished responses." class="local">\Drupal\Core\EventSubscriber\FinishResponseSubscriber</a> @group EventSubscriber

Namespace

Drupal\Tests\Core\EventSubscriber

Code

public function testExistingHeaders() : void {
    $finishSubscriber = new FinishResponseSubscriber($this->languageManager, $this->getConfigFactoryStub(), $this->requestPolicy, $this->responsePolicy, $this->cacheContextsManager, $this->time, FALSE);
    $this->languageManager
        ->method('getCurrentLanguage')
        ->willReturn(new Language([
        'id' => 'en',
    ]));
    $request = $this->createMock(Request::class);
    $response = $this->createMock(Response::class);
    $response->headers = new ResponseHeaderBag();
    $event = new ResponseEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
    $response->headers
        ->set('X-Content-Type-Options', 'foo');
    $response->headers
        ->set('X-Frame-Options', 'DENY');
    $finishSubscriber->onRespond($event);
    $this->assertEquals([
        'en',
    ], $response->headers
        ->all('Content-language'));
    // 'X-Content-Type-Options' will be unconditionally set by core.
    $this->assertEquals([
        'nosniff',
    ], $response->headers
        ->all('X-Content-Type-Options'));
    $this->assertEquals([
        'DENY',
    ], $response->headers
        ->all('X-Frame-Options'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.