You are here

function RestfulDiscoveryTestCase::testOptionsMethod in RESTful 7

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

Test the headers populated in an OPTIONS request.

File

tests/RestfulDiscoveryTestCase.test, line 25
Contains RestfulDiscoveryTestCase

Class

RestfulDiscoveryTestCase
@file Contains RestfulDiscoveryTestCase

Code

function testOptionsMethod() {
  $handler = restful_get_restful_handler('test_articles', 1, 4);

  // 1. Assert Access-Control-Allow-Methods.
  $handler
    ->options('');
  $headers = $handler
    ->getHttpHeaders();
  $this
    ->assertEqual($headers['Access-Control-Allow-Methods'], \RestfulInterface::HEAD, 'Access-Control-Allow-Methods header is populated correctly.');

  // Make sure it returns the appropriate headers for every path.
  $handler
    ->options('1');
  $headers = $handler
    ->getHttpHeaders();
  $this
    ->assertEqual($headers['Access-Control-Allow-Methods'], \RestfulInterface::PATCH . ',' . \RestfulInterface::DELETE, 'Access-Control-Allow-Methods header is populated correctly for different paths.');

  // 2. Assert Accept.
  $this
    ->assertEqual($headers['Accept'], 'application/xml; charset=utf-8', 'Accept header is populated correctly for configured formatter.');
  $handler = restful_get_restful_handler('test_articles', 1, 2);
  $handler
    ->options('');
  $accepted_formats = array();
  foreach ($handler
    ->formatterNames() as $formatter_name) {
    $formatter = restful_get_formatter_handler($formatter_name, $handler);
    $accepted_formats[] = $formatter
      ->getContentTypeHeader();
  }
  $headers = $handler
    ->getHttpHeaders();
  $this
    ->assertEqual($headers['Accept'], implode(',', $accepted_formats), 'Accept header is populated correctly for non configured formatters.');

  // 3. Assert Access-Control-Allow-Origin.
  $response = $this
    ->httpRequest('api/v1.4/test_articles', \RestfulInterface::HEAD);
  $headers = explode("\n", $response['headers']);
  $this
    ->assertTrue(in_array('*', $headers), 'Access-Control-Allow-Origin header is populated correctly.');
}