You are here

protected function RESTTestBase::httpRequest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Tests/RESTTestBase.php \Drupal\rest\Tests\RESTTestBase::httpRequest()

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

Parameters

string|\Drupal\Core\Url $url: A Url object or system path.

string $method: HTTP method, one of GET, POST, PUT or DELETE.

string $body: The body for POST and PUT.

string $mime_type: The MIME type of the transmitted content.

Return value

string The content returned from the request.

23 calls to RESTTestBase::httpRequest()
AuthTest::testRead in core/modules/rest/src/Tests/AuthTest.php
Tests reading from an authenticated resource.
CreateTest::assertCreateEntityInvalidData in core/modules/rest/src/Tests/CreateTest.php
Try to send invalid data that cannot be correctly deserialized.
CreateTest::assertCreateEntityInvalidSerialized in core/modules/rest/src/Tests/CreateTest.php
Send an invalid UUID to trigger the entity validation.
CreateTest::assertCreateEntityNoData in core/modules/rest/src/Tests/CreateTest.php
Try to send no data at all, which does not make sense on POST requests.
CreateTest::assertCreateEntityOverRestApi in core/modules/rest/src/Tests/CreateTest.php
Creates the entity over the REST API.

... See full list

File

core/modules/rest/src/Tests/RESTTestBase.php, line 85
Contains \Drupal\rest\Tests\RESTTestBase.

Class

RESTTestBase
Test helper class that provides a REST client method to send HTTP requests.

Namespace

Drupal\rest\Tests

Code

protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) {
  if (!isset($mime_type)) {
    $mime_type = $this->defaultMimeType;
  }
  if (!in_array($method, array(
    'GET',
    'HEAD',
    'OPTIONS',
    'TRACE',
  ))) {

    // GET the CSRF token first for writing requests.
    $token = $this
      ->drupalGet('rest/session/token');
  }
  $url = $this
    ->buildUrl($url);
  switch ($method) {
    case 'GET':

      // Set query if there are additional GET parameters.
      $curl_options = array(
        CURLOPT_HTTPGET => TRUE,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Accept: ' . $mime_type,
        ),
      );
      break;
    case 'POST':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Content-Type: ' . $mime_type,
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
    case 'PUT':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'PUT',
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Content-Type: ' . $mime_type,
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
    case 'PATCH':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'PATCH',
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'Content-Type: ' . $mime_type,
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
    case 'DELETE':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'DELETE',
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'X-CSRF-Token: ' . $token,
        ),
      );
      break;
  }
  $this->responseBody = $this
    ->curlExec($curl_options);

  // Ensure that any changes to variables in the other thread are picked up.
  $this
    ->refreshVariables();
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->verbose($method . ' request to: ' . $url . '<hr />Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . '<hr />Response headers: ' . nl2br(print_r($headers, TRUE)) . '<hr />Response body: ' . $this->responseBody);
  return $this->responseBody;
}