protected function EntityResourceTestBase::assert406Response in Drupal 10
Same name and namespace in other branches
- 8 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assert406Response()
- 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assert406Response()
Asserts a 406 response… or in some cases a 403 response, because weirdness.
Asserting a 406 response should be easy, but it's not, due to bugs.
Drupal returns a 403 response instead of a 406 response when:
- there is a canonical route, i.e. one that serves HTML
- unless the user is logged in with any non-global authentication provider, because then they tried to access a route that requires the user to be authenticated, but they used an authentication provider that is only accepted for specific routes, and HTML routes never have such specific authentication providers specified. (By default, only 'cookie' is a global authentication provider.)
@todo Remove this in https://www.drupal.org/node/2805279.
Parameters
\Psr\Http\Message\ResponseInterface $response: The response to assert.
2 calls to EntityResourceTestBase::assert406Response()
- EntityResourceTestBase::assertResourceNotAvailable in core/
modules/ rest/ tests/ src/ Functional/ EntityResource/ EntityResourceTestBase.php - Asserts that a resource is unavailable: 404, 406 if it has canonical route.
- EntityResourceTestBase::testGet in core/
modules/ rest/ tests/ src/ Functional/ EntityResource/ EntityResourceTestBase.php - Tests a GET request for an entity, plus edge cases to ensure good DX.
File
- core/
modules/ rest/ tests/ src/ Functional/ EntityResource/ EntityResourceTestBase.php, line 1343
Class
- EntityResourceTestBase
- Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…
Namespace
Drupal\Tests\rest\Functional\EntityResourceCode
protected function assert406Response(ResponseInterface $response) {
if ($this->entity
->hasLinkTemplate('canonical') && ($this->account && static::$auth !== 'cookie')) {
$this
->assertSame(403, $response
->getStatusCode());
}
else {
// This is the desired response.
$this
->assertSame(406, $response
->getStatusCode());
$actual_link_header = $response
->getHeader('Link');
if ($actual_link_header) {
$this
->assertIsArray($actual_link_header);
$expected_type = explode(';', static::$mimeType)[0];
$this
->assertStringContainsString('?_format=' . static::$format . '>; rel="alternate"; type="' . $expected_type . '"', $actual_link_header[0]);
$this
->assertStringContainsString('?_format=foobar>; rel="alternate"', $actual_link_header[0]);
}
}
}