function EntityResourceTestBase::assert406Response

Same name in other branches
  1. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assert406Response()
  2. 8.9.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assert406Response()
  3. 11.x 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 1354

Class

EntityResourceTestBase
Defines a base class for testing all entity resources.

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

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]);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.