You are here

public function RestfulHookMenuTestCase::testVersionNegotiation in RESTful 7.2

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

Test the version negotiation.

File

tests/RestfulHookMenuTestCase.test, line 135
Contains \RestfulHookMenuTestCase.

Class

RestfulHookMenuTestCase

Code

public function testVersionNegotiation() {

  // Fake the HTTP header.
  $test_harness = array(
    array(
      'path' => 'api/v1.1/articles',
      'version_header' => NULL,
      'expected_version' => array(
        1,
        1,
      ),
      'expected_resource' => 'articles',
    ),
    array(
      'path' => 'api/v1/articles',
      'version_header' => NULL,
      'expected_version' => array(
        1,
        7,
      ),
      'expected_resource' => 'articles',
    ),
    array(
      'path' => 'api/articles',
      'version_header' => 'v1',
      'expected_version' => array(
        1,
        7,
      ),
      'expected_resource' => 'articles',
    ),
    array(
      'path' => 'api/articles',
      'version_header' => 'v1.0',
      'expected_version' => array(
        1,
        0,
      ),
      'expected_resource' => 'articles',
    ),
    array(
      'path' => 'api/articles',
      'version_header' => NULL,
      'expected_version' => array(
        2,
        1,
      ),
      'expected_resource' => 'articles',
    ),
  );
  foreach ($test_harness as $test_item) {
    $headers = NULL;
    if (!empty($test_item['version_header'])) {
      $headers = new \Drupal\restful\Http\HttpHeaderBag(array(
        'X-API-Version' => $test_item['version_header'],
      ));
    }
    $request = \Drupal\restful\Http\Request::create($test_item['path'], array(), RequestInterface::METHOD_GET, $headers);
    $resource_manager = new \Drupal\restful\Resource\ResourceManager($request);
    drupal_static_reset('Drupal\\restful\\Resource\\ResourceManager::getVersionFromRequest');
    $this
      ->assertEqual($resource_manager
      ->getVersionFromRequest(), $test_item['expected_version'], sprintf('%s resolves correctly.', $test_item['path']));
    $this
      ->assertEqual($resource_manager
      ->getResourceIdFromRequest(), $test_item['expected_resource'], sprintf('Resource name obtained correctly from %s.', $test_item['path']));
  }
}