You are here

protected function RestfulCurlBaseTestCase::httpRequest in RESTful 7.2

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

Helper function to issue a HTTP request with simpletest's cURL.

Copied and slightly adjusted from the RestWS module.

Parameters

string $url: The URL of the request.

string $method: The HTTP method of the request. Defaults to "GET".

array $body: Either the body for POST and PUT or additional URL parameters for GET.

array $headers: Additional HTTP header parameters.

bool $use_token: Determines if a CSRF token should be retrieved by default for write operations for logged in users. Defaults to TRUE.

Return value

array Array keyed with the "code", "headers", and "body" of the response.

14 calls to RestfulCurlBaseTestCase::httpRequest()
RestfulCsrfTokenTestCase::checkCsrfRequest in tests/RestfulCsrfTokenTestCase.test
Perform requests without, with invalid and with valid CSRF tokens.
RestfulDiscoveryTestCase::testDiscoveryPage in tests/RestfulDiscoveryTestCase.test
Test the discovery page.
RestfulDiscoveryTestCase::testOptionsMethod in tests/RestfulDiscoveryTestCase.test
Test the headers populated in an OPTIONS request.
RestfulExceptionHandleTestCase::testEntityNotFoundDelivery in tests/RestfulExceptionHandleTestCase.test
Test when an entity is not found that a 4XX is returned instead of 500.
RestfulExceptionHandleTestCase::testExceptionHandle in tests/RestfulExceptionHandleTestCase.test
Test converting exceptions into JSON with code, message and description.

... See full list

File

tests/RestfulCurlBaseTestCase.test, line 33
Contains \RestfulCurlBaseTestCase

Class

RestfulCurlBaseTestCase

Code

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,
  );
}