You are here

RestfulExceptionHandleTestCase.test in RESTful 7

Same filename and directory in other branches
  1. 7.2 tests/RestfulExceptionHandleTestCase.test

Contains RestfulExceptionHandleTestCase

File

tests/RestfulExceptionHandleTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulExceptionHandleTestCase
 */
class RestfulExceptionHandleTestCase extends RestfulCurlBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Exception handling',
      'description' => 'Test converting exceptions into JSON with code, message and description.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_example');
  }

  /**
   * Test converting exceptions into JSON with code, message and description.
   *
   * When calling the API via hook_menu(), exceptions should be converted to a
   * valid JSON.
   */
  function testExceptionHandle() {
    $options['query'] = array(
      'sort' => 'wrong_key',
    );
    $this
      ->drupalGet('api/v1.0/articles', $options);
    $expected_result = array(
      'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1',
      'title' => 'The sort wrong_key is not allowed for this path.',
      'status' => 400,
      'detail' => 'Bad Request.',
    );
    $this
      ->assertText(drupal_json_encode($expected_result), 'Exception was converted to JSON.');
    $this
      ->assertResponse('400', 'Correct HTTP code.');
  }

  /**
   * Test when an entity is not found that a 4XX is returned instead of 500.
   */
  function testEntityNotFoundDelivery() {
    $url = 'api/v1.0/articles/1';
    $result = $this
      ->httpRequest($url);
    $body = drupal_json_decode($result['body']);
    $this
      ->assertEqual($result['code'], '422', format_string('422 status code sent for @url url.', array(
      '@url' => $url,
    )));
    $this
      ->assertTrue(strpos($result['headers'], 'application/problem+json;'), '"application/problem+json" found in invalid request.');
    $this
      ->assertEqual($body['title'], 'The entity ID 1 for Articles does not exist.', 'Correct error message.');
  }

}

Classes

Namesort descending Description
RestfulExceptionHandleTestCase @file Contains RestfulExceptionHandleTestCase