You are here

RestfulExceptionHandleTestCase.test in RESTful 7.2

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

Contains RestfulExceptionHandleTestCase.

File

tests/RestfulExceptionHandleTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulExceptionHandleTestCase.
 */

/**
 * Class RestfulExceptionHandleTestCase.
 */
class RestfulExceptionHandleTestCase extends \RestfulCurlBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Exception handling',
      'description' => 'Test converting exceptions into JSON with code, message and description.',
      'group' => 'RESTful',
    );
  }

  /**
   * {@inheritdoc}
   */
  public 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.
   */
  public function testExceptionHandle() {
    $options = array(
      'sort' => 'wrong_key',
    );
    $result = $this
      ->httpRequest('api/v1.0/articles', \Drupal\restful\Http\RequestInterface::METHOD_GET, $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
      ->assertEqual(drupal_json_decode($result['body']), $expected_result);
    $this
      ->assertEqual($result['code'], 400, 'Correct HTTP code.');
  }

  /**
   * Test when an entity is not found that a 4XX is returned instead of 500.
   */
  public 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;') !== FALSE, '"application/problem+json" found in invalid request.');
    $this
      ->assertEqual($body['title'], 'The entity ID 1 does not exist.', 'Correct error message.');
  }

}

Classes

Namesort descending Description
RestfulExceptionHandleTestCase Class RestfulExceptionHandleTestCase.