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