protected function RESTTestBase::httpRequest

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

Parameters

string $url: The relative URL, e.g. "entity/node/1"

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

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

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

3 calls to RESTTestBase::httpRequest()
DBLogTest::testWatchdog in drupal/core/modules/rest/lib/Drupal/rest/Tests/DBLogTest.php
Writes a log messages and retrieves it via the web API.
DeleteTest::testDelete in drupal/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php
Tests several valid and invalid delete requests on all entity types.
ReadTest::testRead in drupal/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php
Tests several valid and invalid read requests on all entity types.

File

drupal/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php, line 36
Definition of Drupal\rest\test\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, $format = 'application/ld+json') {
  switch ($method) {
    case 'GET':

      // Set query if there are additional 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: ' . $format,
        ),
      );
      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: ' . $format,
        ),
      );
      break;
    case 'DELETE':
      $curl_options = array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'DELETE',
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
      );
      break;
  }

  // Include all HTTP headers in the response.
  $curl_options[CURLOPT_HEADER] = TRUE;
  $response = $this
    ->curlExec($curl_options);
  list($header, $body) = explode("\r\n\r\n", $response, 2);
  $header_lines = explode("\r\n", $header);
  foreach ($header_lines as $line) {
    $parts = explode(':', $line, 2);
    $this->responseHeaders[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : '';
  }
  $this
    ->verbose($method . ' request to: ' . $url . '<hr />Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . '<hr />Response headers: ' . $header . '<hr />Response body: ' . $body);
  return $body;
}