class ResponseGeneratorTest
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest
- 10 core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest
- 8.9.x core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php \Drupal\Tests\system\Functional\System\ResponseGeneratorTest
Tests to see if generator header is added.
@group system
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Functional\System\ResponseGeneratorTest extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of ResponseGeneratorTest
File
-
core/
modules/ system/ tests/ src/ Functional/ System/ ResponseGeneratorTest.php, line 14
Namespace
Drupal\Tests\system\Functional\SystemView source
class ResponseGeneratorTest extends BrowserTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'serialization',
'rest',
'node',
'basic_auth',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$account = $this->drupalCreateUser([
'access content',
]);
$this->drupalLogin($account);
}
/**
* Tests to see if generator header is added.
*/
public function testGeneratorHeaderAdded() {
$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);
// Create a cookie-based authentication for the entity:node REST resource.
// @todo Turn this back in to an optional config YAML file in D10 to have an
// example config for REST endpoints and adjust
// core/modules/help_topics/help_topics/core.web_services.html.twig and
// core/core.api.php accordingly.
// See https://www.drupal.org/project/drupal/issues/3049857
$resource_config_values = [
'id' => 'entity.node',
'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
'configuration' => [
'methods' => [
'GET',
],
'formats' => [
'json',
],
'authentication' => [
'cookie',
],
],
];
RestResourceConfig::create($resource_config_values)->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.