You are here

protected function ContentHubSubscriberTestBase::httpRequest in Acquia Content Hub 8

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.

bool $csrf_token: If NULL, a CSRF token will be retrieved and used. If FALSE, omit the X-CSRF-Token request header (to simulate developer error). Otherwise, the passed in value will be used as the value for the X-CSRF-Token request header (to simulate developer error, by sending an invalid CSRF token).

Return value

string The content returned from the request.

5 calls to ContentHubSubscriberTestBase::httpRequest()
ContentHubFiltersTest::allHttpRequestDenied in acquia_contenthub_subscriber/tests/src/Functional/ContentHubFiltersTest.php
Performs all requests to the filters endpoint.
ContentHubFiltersTest::deleteHttpRequest in acquia_contenthub_subscriber/tests/src/Functional/ContentHubFiltersTest.php
Performs a DELETE request on the filters endpoint.
ContentHubFiltersTest::getHttpRequest in acquia_contenthub_subscriber/tests/src/Functional/ContentHubFiltersTest.php
Performs a GET request on filters endpoint.
ContentHubFiltersTest::patchHttpRequest in acquia_contenthub_subscriber/tests/src/Functional/ContentHubFiltersTest.php
Performs a PATCH request on the filters endpoint.
ContentHubFiltersTest::postHttpRequest in acquia_contenthub_subscriber/tests/src/Functional/ContentHubFiltersTest.php
Performs a POST request on the filters endpoint.

File

acquia_contenthub_subscriber/tests/src/Functional/ContentHubSubscriberTestBase.php, line 161

Class

ContentHubSubscriberTestBase
Provides the base class for web tests for Content Hub Subscribers.

Namespace

Drupal\Tests\acquia_contenthub_subscriber\Functional

Code

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

    // GET the CSRF token first for writing requests.
    $requested_token = $this
      ->drupalGet('/session/token');
  }
  $url = $this
    ->buildUrl($url) . '?_format=json';
  $request_options = [];
  $client = \Drupal::httpClient();
  switch ($method) {
    case 'GET':
      $request_options = [
        'headers' => [
          'Accept' => $mime_type,
        ],
        'curl' => [
          CURLOPT_CUSTOMREQUEST => 'GET',
        ],
      ];
      break;
    case 'POST':
      $headers = [];
      $headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
      $headers['Content-Type'] = $mime_type !== 'none' ? $mime_type : NULL;
      $request_options = [
        'timeout' => 10,
        'body' => $body,
        'headers' => $headers,
        'curl' => [
          CURLOPT_CUSTOMREQUEST => 'POST',
        ],
      ];
      break;
    case 'PATCH':
      $headers = [];
      $headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
      $headers['Content-Type'] = $mime_type !== 'none' ? $mime_type : NULL;
      $request_options = [
        'timeout' => 10,
        'body' => $body,
        'headers' => $headers,
        'curl' => [
          CURLOPT_CUSTOMREQUEST => 'PATCH',
        ],
      ];
      break;
    case 'DELETE':
      $headers = [];
      $headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
      $request_options = [
        'timeout' => 10,
        'headers' => $headers,
        'curl' => [
          CURLOPT_CUSTOMREQUEST => 'DELETE',
        ],
      ];
      break;
  }
  $request_options[RequestOptions::ALLOW_REDIRECTS] = TRUE;
  try {
    $response = $client
      ->request($method, $url, $request_options);
    $this->responseBody = $response
      ->getBody();
  } catch (RequestException $e) {
    dump($e
      ->getCode() . ": " . $e
      ->getMessage());
  }

  // Ensure that any changes to variables in the other thread are picked up.
  $this
    ->refreshVariables();
  $headers = $this
    ->getSession()
    ->getResponseHeaders();
  dump($method . ' request to: ' . $url . '<hr />Code: ' . $response
    ->getStatusCode() . (isset($request_options['headers']) ? '<hr />Request headers: ' . nl2br(print_r($request_options['headers'], TRUE)) : '') . (isset($request_options['body']) ? '<hr />Request body: ' . nl2br(print_r($request_options['body'], TRUE)) : '') . '<hr />Response headers: ' . nl2br(print_r($headers, TRUE)) . '<hr />Response body: ' . $this->responseBody);
  return $this->responseBody;
}