You are here

public function RestfulDiscoveryTestCase::testOptionsMethod in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulDiscoveryTestCase.test \RestfulDiscoveryTestCase::testOptionsMethod()

Test the headers populated in an OPTIONS request.

File

tests/RestfulDiscoveryTestCase.test, line 53
Contains RestfulDiscoveryTestCase

Class

RestfulDiscoveryTestCase
Class RestfulDiscoveryTestCase.

Code

public function testOptionsMethod() {

  // 1. Assert Access-Control-Allow-Methods.
  $response = $this
    ->httpRequest('api/v1.4/test_articles', RequestInterface::METHOD_OPTIONS);
  $this
    ->assertTrue(strpos($response['headers'], sprintf('access-control-allow-methods: %s', implode(', ', array(
    RequestInterface::METHOD_HEAD,
    RequestInterface::METHOD_OPTIONS,
  )))) !== FALSE, 'Access-Control-Allow-Methods header is populated correctly.');

  // Make sure it returns the appropriate headers for every path.
  $response = $this
    ->httpRequest('api/v1.4/test_articles/1', RequestInterface::METHOD_OPTIONS);
  $this
    ->assertTrue(strpos($response['headers'], sprintf('access-control-allow-methods: %s', implode(', ', array(
    RequestInterface::METHOD_PATCH,
    RequestInterface::METHOD_DELETE,
    RequestInterface::METHOD_OPTIONS,
  )))) !== FALSE, 'Access-Control-Allow-Methods header is populated correctly for different paths.');

  // 2. Assert Accept.
  // List the content types the route accepts.
  $this
    ->assertTrue(strpos($response['headers'], 'accept: application/xml; charset=utf-8') !== FALSE, 'Accept header is populated correctly for configured formatter.');
  $response = $this
    ->httpRequest('api/v1.2/test_articles', RequestInterface::METHOD_OPTIONS);
  $this
    ->assertTrue(strpos($response['headers'], 'application/hal+json') !== FALSE, 'Accept header is populated correctly for non configured formatters.');
  $this
    ->assertTrue(strpos($response['headers'], 'application/json') !== FALSE, 'Accept header is populated correctly for non configured formatters.');
  $this
    ->assertTrue(strpos($response['headers'], 'application/vnd.api+json') !== FALSE, 'Accept header is populated correctly for non configured formatters.');
  $this
    ->assertTrue(strpos($response['headers'], 'application/drupal.single+json') !== FALSE, 'Accept header is populated correctly for non configured formatters.');
  $this
    ->assertTrue(strpos($response['headers'], 'application/xml') !== FALSE, 'Accept header is populated correctly for non configured formatters.');

  // 3. Assert Access-Control-Allow-Origin.
  $response = $this
    ->httpRequest('api/v1.4/test_articles', RequestInterface::METHOD_HEAD);
  $this
    ->assertTrue(strpos($response['headers'], 'access-control-allow-origin: *') !== FALSE, 'Accept header is populated correctly for non configured formatters.');

  // 4. Assert Access.
  $response = $this
    ->httpRequest('api/v1.4/test_articles/1', RequestInterface::METHOD_HEAD);
  $this
    ->assertTrue($response['code'], 400, 'Access is denied for unsupported HTTP methods.');
}