annotate core/modules/jsonapi/tests/src/Functional/JsonApiRequestTestTrait.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\Tests\jsonapi\Functional;
Chris@5 4
Chris@5 5 use Behat\Mink\Driver\BrowserKitDriver;
Chris@5 6 use Drupal\Core\Url;
Chris@5 7 use GuzzleHttp\RequestOptions;
Chris@5 8
Chris@5 9 /**
Chris@5 10 * Boilerplate for JSON:API Functional tests' HTTP requests.
Chris@5 11 *
Chris@5 12 * @internal
Chris@5 13 */
Chris@5 14 trait JsonApiRequestTestTrait {
Chris@5 15
Chris@5 16 /**
Chris@5 17 * Performs a HTTP request. Wraps the Guzzle HTTP client.
Chris@5 18 *
Chris@5 19 * Why wrap the Guzzle HTTP client? Because we want to keep the actual test
Chris@5 20 * code as simple as possible, and hence not require them to specify the
Chris@5 21 * 'http_errors = FALSE' request option, nor do we want them to have to
Chris@5 22 * convert Drupal Url objects to strings.
Chris@5 23 *
Chris@5 24 * We also don't want to follow redirects automatically, to ensure these tests
Chris@5 25 * are able to detect when redirects are added or removed.
Chris@5 26 *
Chris@5 27 * @param string $method
Chris@5 28 * HTTP method.
Chris@5 29 * @param \Drupal\Core\Url $url
Chris@5 30 * URL to request.
Chris@5 31 * @param array $request_options
Chris@5 32 * Request options to apply.
Chris@5 33 *
Chris@5 34 * @return \Psr\Http\Message\ResponseInterface
Chris@5 35 * The response.
Chris@5 36 *
Chris@5 37 * @see \GuzzleHttp\ClientInterface::request()
Chris@5 38 */
Chris@5 39 protected function request($method, Url $url, array $request_options) {
Chris@5 40 $this->refreshVariables();
Chris@5 41 $request_options[RequestOptions::HTTP_ERRORS] = FALSE;
Chris@5 42 $request_options[RequestOptions::ALLOW_REDIRECTS] = FALSE;
Chris@5 43 $request_options = $this->decorateWithXdebugCookie($request_options);
Chris@5 44 $client = $this->getSession()->getDriver()->getClient()->getClient();
Chris@5 45 return $client->request($method, $url->setAbsolute(TRUE)->toString(), $request_options);
Chris@5 46 }
Chris@5 47
Chris@5 48 /**
Chris@5 49 * Adds the Xdebug cookie to the request options.
Chris@5 50 *
Chris@5 51 * @param array $request_options
Chris@5 52 * The request options.
Chris@5 53 *
Chris@5 54 * @return array
Chris@5 55 * Request options updated with the Xdebug cookie if present.
Chris@5 56 */
Chris@5 57 protected function decorateWithXdebugCookie(array $request_options) {
Chris@5 58 $session = $this->getSession();
Chris@5 59 $driver = $session->getDriver();
Chris@5 60 if ($driver instanceof BrowserKitDriver) {
Chris@5 61 $client = $driver->getClient();
Chris@5 62 foreach ($client->getCookieJar()->all() as $cookie) {
Chris@5 63 if (isset($request_options[RequestOptions::HEADERS]['Cookie'])) {
Chris@5 64 $request_options[RequestOptions::HEADERS]['Cookie'] .= '; ' . $cookie->getName() . '=' . $cookie->getValue();
Chris@5 65 }
Chris@5 66 else {
Chris@5 67 $request_options[RequestOptions::HEADERS]['Cookie'] = $cookie->getName() . '=' . $cookie->getValue();
Chris@5 68 }
Chris@5 69 }
Chris@5 70 }
Chris@5 71 return $request_options;
Chris@5 72 }
Chris@5 73
Chris@5 74 }