public function MockHttpClientMiddleware::__invoke in Thunder 8.5
Same name and namespace in other branches
- 6.2.x tests/modules/thunder_test_mock_request/src/MockHttpClientMiddleware.php \Drupal\thunder_test_mock_request\MockHttpClientMiddleware::__invoke()
- 6.1.x tests/modules/thunder_test_mock_request/src/MockHttpClientMiddleware.php \Drupal\thunder_test_mock_request\MockHttpClientMiddleware::__invoke()
HTTP middleware that adds the next mocked response.
File
- tests/
modules/ thunder_test_mock_request/ src/ MockHttpClientMiddleware.php, line 68
Class
- MockHttpClientMiddleware
- Sets the mocked responses.
Namespace
Drupal\thunder_test_mock_requestCode
public function __invoke() {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$items = $this->state
->get(static::class, []);
$url = (string) $request
->getUri();
if (!empty($items[$url])) {
$response = new Response($items[$url]['status'], $items[$url]['headers'], $items[$url]['body']);
// @phpstan-ignore-next-line
return promise_for($response);
}
elseif (strstr($this->request
->getHttpHost(), $request
->getUri()
->getHost()) === FALSE) {
throw new \Exception(sprintf("No response for %s defined. See MockHttpClientMiddleware::addUrlResponse().", $url));
}
return $handler($request, $options);
};
};
}