You are here

RestfulCurlBaseTestCase.test in RESTful 7.2

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

File

tests/RestfulCurlBaseTestCase.test
View source
<?php

/**
 * @file
 * Contains \RestfulCurlBaseTestCase
 */
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Http\Request;
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 = RequestInterface::METHOD_GET, $body = NULL, $headers = array(), $use_token = TRUE) {
    $format = 'json';
    $curl_options = array();
    switch ($method) {
      case RequestInterface::METHOD_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 RequestInterface::METHOD_HEAD:
      case RequestInterface::METHOD_OPTIONS:

        // 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 => $method,
          CURLOPT_URL => url($url, $options),
          CURLOPT_NOBODY => FALSE,
        );
        break;
      case RequestInterface::METHOD_POST:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_POST => TRUE,
          CURLOPT_POSTFIELDS => is_array($body) ? http_build_query($body) : $body,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        if (empty($headers['Content-Type'])) {
          $headers['Content-Type'] = 'application/' . $format;
        }
        break;
      case RequestInterface::METHOD_PUT:
      case RequestInterface::METHOD_PATCH:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => $method,
          CURLOPT_POSTFIELDS => is_array($body) ? http_build_query($body) : $body,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        if (empty($headers['Content-Type'])) {
          $headers['Content-Type'] = 'application/' . $format;
        }
        break;
      case RequestInterface::METHOD_DELETE:
        $curl_options = array(
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => RequestInterface::METHOD_DELETE,
          CURLOPT_URL => url($url, array(
            'absolute' => TRUE,
          )),
          CURLOPT_NOBODY => FALSE,
        );
        break;
    }
    $curl_options += array(
      CURLOPT_HTTPHEADER => array(),
    );
    if (Request::isWriteMethod($method) && $use_token) {

      // Add CSRF token for write operations.
      if (empty($this->csrfToken)) {
        $response = $this
          ->drupalGet('api/session/token');
        $result = drupal_json_decode($response);
        if (!empty($result['X-CSRF-Token'])) {
          $this->csrfToken = $result['X-CSRF-Token'];
          $headers['X-CSRF-Token'] = $result['X-CSRF-Token'];
        }
      }
      else {
        $headers['X-CSRF-Token'] = $this->csrfToken;
      }
    }
    foreach ($headers as $key => $value) {
      $curl_options[CURLOPT_HTTPHEADER][] = $key . ': ' . $value;
    }
    $response = $this
      ->curlExec($curl_options);
    $response_headers = array();
    foreach ($this
      ->drupalGetHeaders() as $header_name => $header_value) {
      $response_headers[] = $header_name . ': ' . $header_value;
    }
    $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