function ResponseGeneratorTest::testGeneratorHeaderAdded

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest::testGeneratorHeaderAdded()
  2. 8.9.x core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest::testGeneratorHeaderAdded()
  3. 11.x core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest::testGeneratorHeaderAdded()

Tests to see if generator header is added.

File

core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php, line 41

Class

ResponseGeneratorTest
Tests to see if generator header is added.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testGeneratorHeaderAdded() : void {
    $node = $this->drupalCreateNode();
    [
        $version,
    ] = explode('.', \Drupal::VERSION, 2);
    $expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';
    // Check to see if the header is added when viewing an HTML page.
    $this->drupalGet($node->toUrl());
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->assertSession()
        ->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);
    // Check to see if the header is also added for a non-successful response.
    $this->drupalGet('llama');
    $this->assertSession()
        ->statusCodeEquals(404);
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->assertSession()
        ->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);
    // Enable cookie-based authentication for the entity:node REST resource.
    $resource_config = RestResourceConfig::load('entity.node');
    $configuration = $resource_config->get('configuration');
    $configuration['authentication'][] = 'cookie';
    $resource_config->set('configuration', $configuration)
        ->save();
    $this->rebuildAll();
    // Check to see if the header is also added for a non-HTML request.
    $this->drupalGet($node->toUrl()
        ->setOption('query', [
        '_format' => 'json',
    ]));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'application/json');
    $this->assertSession()
        ->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);
}

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