ResponseGeneratorTest.php in Drupal 10
File
core/modules/system/tests/src/Functional/System/ResponseGeneratorTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\rest\Entity\RestResourceConfig;
use Drupal\Tests\BrowserTestBase;
class ResponseGeneratorTest extends BrowserTestBase {
protected static $modules = [
'serialization',
'rest',
'node',
'basic_auth',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$account = $this
->drupalCreateUser([
'access content',
]);
$this
->drupalLogin($account);
}
public function testGeneratorHeaderAdded() {
$node = $this
->drupalCreateNode();
[
$version,
] = explode('.', \Drupal::VERSION, 2);
$expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
$this
->assertSession()
->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);
$this
->drupalGet('llama');
$this
->assertSession()
->statusCodeEquals(404);
$this
->assertSession()
->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
$this
->assertSession()
->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);
$resource_config = RestResourceConfig::load('entity.node');
$configuration = $resource_config
->get('configuration');
$configuration['authentication'][] = 'cookie';
$resource_config
->set('configuration', $configuration)
->save();
$this
->rebuildAll();
$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);
}
}