You are here

public function ResourceTest::testFormats 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::testFormats()

Tests that a resource without formats cannot be enabled.

File

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

Class

ResourceTest
Tests the structure of a REST resource.

Namespace

Drupal\rest\Tests

Code

public function testFormats() {
  $settings = array(
    'entity:entity_test' => array(
      'GET' => array(
        'supported_auth' => array(
          'basic_auth',
        ),
      ),
    ),
  );

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

  // Verify that accessing the resource returns 406.
  $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();
}