BasicAuthResourceTestTrait.php in Drupal 9
File
core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php
View source
<?php
namespace Drupal\Tests\rest\Functional;
use Drupal\Core\Url;
use Psr\Http\Message\ResponseInterface;
trait BasicAuthResourceTestTrait {
protected function getAuthenticationRequestOptions($method) {
return [
'headers' => [
'Authorization' => 'Basic ' . base64_encode($this->account->name->value . ':' . $this->account->passRaw),
],
];
}
protected function assertResponseWhenMissingAuthentication($method, ResponseInterface $response) {
if ($method !== 'GET') {
return $this
->assertResourceErrorResponse(401, 'No authentication credentials provided.', $response);
}
$expected_page_cache_header_value = $method === 'GET' ? 'MISS' : FALSE;
$expected_cacheability = $this
->getExpectedUnauthorizedAccessCacheability()
->addCacheableDependency($this
->getExpectedUnauthorizedEntityAccessCacheability(FALSE))
->addCacheableDependency($this
->config('system.site'))
->addCacheTags([
'config:user.role.anonymous',
]);
if (!in_array('user.roles', $expected_cacheability
->getCacheContexts(), TRUE)) {
$expected_cacheability
->addCacheContexts([
'user.roles:anonymous',
]);
}
$this
->assertResourceErrorResponse(401, 'No authentication credentials provided.', $response, $expected_cacheability
->getCacheTags(), $expected_cacheability
->getCacheContexts(), $expected_page_cache_header_value, FALSE);
}
protected function assertAuthenticationEdgeCases($method, Url $url, array $request_options) {
}
}