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