You are here

public function TestHttpClientMiddleware::__invoke in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware::__invoke()

HTTP middleware that replaces the user agent for simpletest requests.

File

core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php, line 22
Contains \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware.

Class

TestHttpClientMiddleware
Overrides the User-Agent HTTP header for outbound HTTP requests.

Namespace

Drupal\Core\Test\HttpClientMiddleware

Code

public function __invoke() {

  // If the database prefix is being used by SimpleTest to run the tests in a copied
  // database then set the user-agent header to the database prefix so that any
  // calls to other Drupal pages will run the SimpleTest prefixed database. The
  // user-agent is used to ensure that multiple testing sessions running at the
  // same time won't interfere with each other as they would if the database
  // prefix were stored statically in a file or database variable.
  return function ($handler) {
    return function (RequestInterface $request, array $options) use ($handler) {
      if ($test_prefix = drupal_valid_test_ua()) {
        $request = $request
          ->withHeader('User-Agent', drupal_generate_test_ua($test_prefix));
      }
      return $handler($request, $options);
    };
  };
}