function AnnounceTestHttpClientMiddleware::__invoke

Same name and namespace in other branches
  1. 10 core/modules/announcements_feed/tests/modules/announce_feed_test/src/AnnounceTestHttpClientMiddleware.php \Drupal\announce_feed_test\AnnounceTestHttpClientMiddleware::__invoke()

HTTP middleware that replaces request endpoint for a test one.

File

core/modules/announcements_feed/tests/modules/announce_feed_test/src/AnnounceTestHttpClientMiddleware.php, line 19

Class

AnnounceTestHttpClientMiddleware
Overrides the requested endpoint when running tests.

Namespace

Drupal\announce_feed_test

Code

public function __invoke() : \Closure {
    return function ($handler) {
        return function (RequestInterface $request, array $options) use ($handler) : PromiseInterface {
            $test_end_point = \Drupal::state()->get('announce_test_endpoint');
            if ($test_end_point && str_contains($request->getUri(), '://www.drupal.org/announcements.json')) {
                // Only override $uri if it matches the advisories JSON feed to avoid
                // changing any other uses of the 'http_client' service during tests with
                // this module installed.
                $request = $request->withUri(new Uri($test_end_point));
            }
            return $handler($request, $options);
        };
    };
}

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