You are here

public function ResourceTest::testAuthentication in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Tests/ResourceTest.php \Drupal\rest\Tests\ResourceTest::testAuthentication()

Tests that a resource without authentication cannot be enabled.

File

core/modules/rest/src/Tests/ResourceTest.php, line 82
Contains \Drupal\rest\Tests\ResourceTest.

Class

ResourceTest
Tests the structure of a REST resource.

Namespace

Drupal\rest\Tests

Code

public function testAuthentication() {
  $settings = array(
    'entity:entity_test' => array(
      'GET' => array(
        'supported_formats' => array(
          'hal_json',
        ),
      ),
    ),
  );

  // Attempt to enable the resource.
  $this->config
    ->set('resources', $settings);
  $this->config
    ->save();
  $this
    ->rebuildCache();

  // Verify that accessing the resource returns 401.
  $response = $this
    ->httpRequest($this->entity
    ->urlInfo()
    ->setRouteParameter('_format', $this->defaultFormat), 'GET');

  // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
  // non-REST route a match, but a lower quality one: no format restrictions
  // means there's always a match and hence when there is no matching REST
  // route, the non-REST route is used, but can't render into
  // application/hal+json, so it returns a 406.
  $this
    ->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
  $this
    ->curlClose();
}