trait ApiRequestTrait

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/ApiRequestTrait.php \Drupal\Tests\ApiRequestTrait

Boilerplate for API Functional tests' HTTP requests.

@internal

Hierarchy

2 files declare their use of ApiRequestTrait
JsonApiRequestTestTrait.php in core/modules/jsonapi/tests/src/Functional/JsonApiRequestTestTrait.php
LinksetControllerTestBase.php in core/modules/system/tests/src/Functional/Menu/LinksetControllerTestBase.php

File

core/tests/Drupal/Tests/ApiRequestTrait.php, line 16

Namespace

Drupal\Tests
View source
trait ApiRequestTrait {
  
  /**
   * Performs an HTTP request. Wraps the Guzzle HTTP client.
   *
   * Why wrap the Guzzle HTTP client? Because we want to keep the actual test
   * code as simple as possible, and hence not require them to specify the
   * 'http_errors = FALSE' request option, nor do we want them to have to
   * convert Drupal Url objects to strings.
   *
   * We also don't want to follow redirects automatically, to ensure these tests
   * are able to detect when redirects are added or removed.
   *
   * @param string $method
   *   HTTP method.
   * @param \Drupal\Core\Url $url
   *   URL to request.
   * @param array $request_options
   *   Request options to apply.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   The response.
   *
   * @see \GuzzleHttp\ClientInterface::request()
   */
  protected function makeApiRequest($method, Url $url, array $request_options) {
    // HEAD requests do not have bodies. If one is specified, Guzzle will not
    // ignore it and the request will be treated as GET with an overridden
    // method string, and libcurl will expect to read a response body.
    if ($method === 'HEAD' && array_key_exists('body', $request_options)) {
      unset($request_options['body']);
    }
    $this->refreshVariables();
    $request_options[RequestOptions::HTTP_ERRORS] = FALSE;
    $request_options[RequestOptions::ALLOW_REDIRECTS] = FALSE;
    $request_options = $this->decorateWithXdebugCookie($request_options);
    $client = $this->getSession()
      ->getDriver()
      ->getClient()
      ->getClient();
    return $client->request($method, $url->setAbsolute(TRUE)
      ->toString(), $request_options);
  }
  
  /**
   * Adds the Xdebug cookie to the request options.
   *
   * @param array $request_options
   *   The request options.
   *
   * @return array
   *   Request options updated with the Xdebug cookie if present.
   */
  protected function decorateWithXdebugCookie(array $request_options) {
    $session = $this->getSession();
    $driver = $session->getDriver();
    if ($driver instanceof BrowserKitDriver) {
      $client = $driver->getClient();
      foreach ($client->getCookieJar()
        ->all() as $cookie) {
        if (isset($request_options[RequestOptions::HEADERS]['Cookie'])) {
          $request_options[RequestOptions::HEADERS]['Cookie'] .= '; ' . $cookie->getName() . '=' . $cookie->getValue();
        }
        else {
          $request_options[RequestOptions::HEADERS]['Cookie'] = $cookie->getName() . '=' . $cookie->getValue();
        }
      }
    }
    return $request_options;
  }

}

Members

Title Sort descending Modifiers Object type Summary
ApiRequestTrait::decorateWithXdebugCookie protected function Adds the Xdebug cookie to the request options.
ApiRequestTrait::makeApiRequest protected function Performs an HTTP request. Wraps the Guzzle HTTP client.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.