function ApiRequestTrait::makeApiRequest

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/ApiRequestTrait.php \Drupal\Tests\ApiRequestTrait::makeApiRequest()

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.

Parameters

string $method: HTTP method.

\Drupal\Core\Url $url: URL to request.

array $request_options: Request options to apply.

Return value

\Psr\Http\Message\ResponseInterface The response.

See also

\GuzzleHttp\ClientInterface::request()

3 calls to ApiRequestTrait::makeApiRequest()
DbLogResourceTest::testWatchdog in core/modules/dblog/tests/src/Functional/DbLogResourceTest.php
Writes a log messages and retrieves it via the REST API.
LinksetControllerTestBase::doRequest in core/modules/system/tests/src/Functional/Menu/LinksetControllerTestBase.php
Sends a request to the kernel and makes basic response assertions.
RestJsonApiUnsupportedTest::testApiJsonNotSupportedInRest in core/modules/jsonapi/tests/src/Functional/RestJsonApiUnsupportedTest.php
Deploying a REST resource using api_json format results in 400 responses.

File

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

Class

ApiRequestTrait
Boilerplate for API Functional tests' HTTP requests.

Namespace

Drupal\Tests

Code

protected function makeApiRequest($method, Url $url, array $request_options) : ResponseInterface {
  // 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);
}

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