3 namespace Drupal\system\Tests\System;
5 use Drupal\rest\Tests\RESTTestBase;
8 * Tests to see if generator header is added.
12 class ResponseGeneratorTest extends RESTTestBase {
19 public static $modules = ['hal', 'rest', 'node', 'basic_auth'];
24 protected function setUp() {
26 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
28 $permissions = $this->entityPermissions('node', 'view');
29 $account = $this->drupalCreateUser($permissions);
30 $this->drupalLogin($account);
34 * Test to see if generator header is added.
36 public function testGeneratorHeaderAdded() {
38 $node = $this->drupalCreateNode();
40 list($version) = explode('.', \Drupal::VERSION, 2);
41 $expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';
43 // Check to see if the header is added when viewing a normal content page
44 $this->drupalGet($node->urlInfo());
45 $this->assertResponse(200);
46 $this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
47 $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
49 // Check to see if the header is also added for a non-successful response
50 $this->drupalGet('llama');
51 $this->assertResponse(404);
52 $this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
53 $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
55 // Enable cookie-based authentication for the entity:node REST resource.
56 /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
57 $resource_config = $this->resourceConfigStorage->load('entity.node');
58 $configuration = $resource_config->get('configuration');
59 $configuration['authentication'][] = 'cookie';
60 $resource_config->set('configuration', $configuration)->save();
61 $this->rebuildCache();
63 // Tests to see if this also works for a non-html request
64 $this->httpRequest($node->urlInfo()->setOption('query', ['_format' => 'hal_json']), 'GET');
65 $this->assertResponse(200);
66 $this->assertEqual('application/hal+json', $this->drupalGetHeader('Content-Type'));
67 $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));