function FinishResponseSubscriberTest::testExistingHeaders

Same name in other branches
  1. 11.x 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 \Drupal\Core\EventSubscriber\FinishResponseSubscriber @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.