You are here

public function RestfulViewEntityTestCase::testHeaders in RESTful 7.2

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

Test the generation of the Vary headers.

File

tests/RestfulViewEntityTestCase.test, line 373
Contains \RestfulViewEntityTestCase.

Class

RestfulViewEntityTestCase

Code

public function testHeaders() {
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));

  // When there is no header version passed in there is no need of Vary.
  $response = $this
    ->httpRequest('api/v1.1/articles');
  $this
    ->assertFalse(preg_match('/X-API-Version/', $response['headers']), 'The Vary header was not added.');

  // Make sure that the version header is present in the response.
  $this
    ->assertTrue(preg_match('/v1\\.1/', $response['headers']), 'The Vary header was added.');
  $response = $this
    ->httpRequest('api/articles', RequestInterface::METHOD_GET, NULL, array(
    'X-API-Version' => 'v1.1',
  ));
  $this
    ->assertTrue(preg_match('/X-API-Version/', $response['headers']), 'The Vary header was added.');

  // Make sure that the version header is present in the response.
  $this
    ->assertTrue(preg_match('/v1\\.1/', $response['headers']), 'The Vary header was added.');

  // Test that if there is no explicit formatter in the plugin definition then
  // it is selected based on the Accept header.
  variable_set('restful_default_output_formatter', 'invalid');
  $response = $this
    ->httpRequest('api/v1.0/articles/' . $node->nid, RequestInterface::METHOD_GET, NULL, array(
    'Accept' => 'application/hal+json',
  ));
  $this
    ->assertNotNull(drupal_json_decode($response['body']), 'JSON output detected.');

  // Test XML selection.
  $response = $this
    ->httpRequest('api/v1.0/articles/' . $node->nid, RequestInterface::METHOD_GET, NULL, array(
    'Accept' => 'application/xml',
  ));
  $this
    ->assertNotNull(new SimpleXMLElement($response['body']), 'XML output detected.');

  // Test wildcard selection.
  $response = $this
    ->httpRequest('api/v1.0/articles/' . $node->nid, RequestInterface::METHOD_GET, NULL, array(
    'Accept' => 'application/*',
  ));
  $this
    ->assertEqual($response['code'], 200, 'Some output format detected for wildcard Accept.');

  // Test that plugin definition takes precedence.
  $response = $this
    ->httpRequest('api/v1.6/articles/' . $node->nid, RequestInterface::METHOD_GET, NULL, array(
    'Accept' => 'application/hal+json',
  ));
  $this
    ->assertNotNull(new SimpleXMLElement($response['body']), 'Plugin definition takes precedence.');

  // The following should resolve to the 'invalid' formatter. It should raise
  // an exception.
  $response = $this
    ->httpRequest('api/v1.0/articles/' . $node->nid, RequestInterface::METHOD_GET, NULL, array(
    'Accept' => 'non-existing',
  ));
  $this
    ->assertEqual($response['code'], 503, 'Error shown for invalid formatter.');
}