You are here

protected function RestWSTestCase::httpRequest in RESTful Web Services 7.2

Same name and namespace in other branches
  1. 7 restws.test \RestWSTestCase::httpRequest()

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

Parameters

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

9 calls to RestWSTestCase::httpRequest()
RestWSTestCase::testBadInputFormat in ./restws.test
Tests access to restricted input formats.
RestWSTestCase::testBadRequests in ./restws.test
Tests bad requests.
RestWSTestCase::testCRUD in ./restws.test
CRUD tests for nodes.
RestWSTestCase::testErrors in ./restws.test
Test requests to non-existing resources and other errors.
RestWSTestCase::testFieldAccess in ./restws.test
Test field level access restrictions.

... See full list

File

./restws.test, line 579
RESTful web services tests.

Class

RestWSTestCase
@file RESTful web services tests.

Code

protected function httpRequest($url, $method, $account = NULL, $body = NULL, $format = 'json') {
  if (isset($account)) {
    unset($this->curlHandle);
    $this
      ->drupalLogin($account);
  }
  if (in_array($method, array(
    'POST',
    'PUT',
    'DELETE',
  ))) {

    // GET the CSRF token first for writing requests.
    $token = $this
      ->drupalGet('restws/session/token');
  }
  switch ($method) {
    case '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 'POST':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/' . $format,
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
    case 'PUT':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'PUT',
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/' . $format,
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
    case 'DELETE':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'DELETE',
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
  }
  $response = $this
    ->curlExec($curl_options);
  $headers = $this
    ->drupalGetHeaders();
  $headers = implode("\n", $headers);
  $this
    ->verbose($method . ' request to: ' . $url . '<hr />Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . '<hr />Response headers: ' . $headers . '<hr />Response body: ' . $response);
  return $response;
}