You are here

RestfulCurlBaseTestCase.test in RESTful 7

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

File

tests/RestfulCurlBaseTestCase.test
View source
<?php

/**
 * Contains RestfulCurlBaseTestCase
 */
class RestfulCurlBaseTestCase extends DrupalWebTestCase {

  /**
   * Helper function to issue a HTTP request with simpletest's cURL.
   *
   * Copied and slightly adjusted from the RestWS module.
   *
   * @param string $url
   *   The URL of the request.
   * @param string $method
   *   The HTTP method of the request. Defaults to "GET".
   * @param array $body
   *   Either the body for POST and PUT or additional URL parameters for GET.
   * @param array $headers
   *   Additional HTTP header parameters.
   * @param bool $use_token
   *   Determines if a CSRF token should be retrieved by default for write
   *   operations for logged in users. Defaults to TRUE.
   *
   * @return array
   *   Array keyed with the "code", "headers", and "body" of the response.
   */
  protected function httpRequest($url, $method = \RestfulInterface::GET, $body = NULL, $headers = array(), $use_token = TRUE) {
    $format = 'json';
    switch ($method) {
      case \RestfulInterface::GET:

        // Set query if there are addition GET parameters.
        $options = isset($body) ? array(
          'absolute' => TRUE,
          'query' => $body,
        ) : array(
          'absolute' => TRUE,
        );
        $curl_options = array(
          CURLOPT_HTTPGET => TRUE,
          CURLOPT_URL => url($url, $options),
          CURLOPT_NOBODY => FALSE,
        );
        break;
      case \RestfulInterface::HEAD:

        // Set query if there are addition GET parameters.
        $options = isset($body) ? array(
          'absolute' => TRUE,
          'query' => $body,
        ) : array(
          'absolute' => TRUE,
        );
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => \RestfulInterface::HEAD,
          CURLOPT_URL => url($url, $options),
          CURLOPT_NOBODY => FALSE,
        );
        break;
      case \RestfulInterface::POST:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_POST => TRUE,
          CURLOPT_POSTFIELDS => $body,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        if (empty($headers['Content-Type'])) {
          $headers['Content-Type'] = 'application/' . $format;
          $curl_options[CURLOPT_POSTFIELDS] = http_build_query($body);
        }
        break;
      case \RestfulInterface::PUT:
      case \RestfulInterface::PATCH:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => $method,
          CURLOPT_POSTFIELDS => $body,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        if (empty($headers['Content-Type'])) {
          $headers['Content-Type'] = 'application/' . $format;
          $curl_options[CURLOPT_POSTFIELDS] = http_build_query($body);
        }
        break;
      case \RestfulInterface::DELETE:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => \RestfulInterface::DELETE,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        break;
    }
    $curl_options += array(
      CURLOPT_HTTPHEADER => array(),
    );
    if ($this->loggedInUser && \RestfulBase::isWriteMethod($method, FALSE) && $use_token) {

      // Add CSRF token for write operations.
      if (empty($this->csrfToken)) {
        $response = $this
          ->drupalGet('api/session/token');
        $result = drupal_json_decode($response);
        $this->csrfToken = $result['X-CSRF-Token'];
      }
      $headers['X-CSRF-Token'] = $this->csrfToken;
    }
    foreach ($headers as $key => $value) {
      $curl_options[CURLOPT_HTTPHEADER][] = $key . ': ' . $value;
    }
    $response = $this
      ->curlExec($curl_options);
    $response_headers = $this
      ->drupalGetHeaders();
    $response_headers = implode("\n", $response_headers);
    $code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
    $this
      ->verbose($method . ' request to: ' . $url . '<hr />Code: ' . $code . '<hr />Response headers: ' . $response_headers . '<hr />Response body: ' . $response);
    return array(
      'code' => $code,
      'headers' => $response_headers,
      'body' => $response,
    );
  }

}

Classes

Namesort descending Description
RestfulCurlBaseTestCase Contains RestfulCurlBaseTestCase