class TestHttpClientMiddleware

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware
  2. 10 core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware
  3. 11.x core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware

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

Hierarchy

Expanded class hierarchy of TestHttpClientMiddleware

1 file declares its use of TestHttpClientMiddleware
InstallerTestBase.php in core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php

File

core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php, line 12

Namespace

Drupal\Core\Test\HttpClientMiddleware
View source
class TestHttpClientMiddleware {
    
    /**
     * {@inheritdoc}
     *
     * HTTP middleware that replaces the user agent for simpletest requests.
     */
    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)->then(function (ResponseInterface $response) {
                    if (!drupal_valid_test_ua()) {
                        return $response;
                    }
                    $headers = $response->getHeaders();
                    foreach ($headers as $header_name => $header_values) {
                        if (preg_match('/^X-Drupal-Assertion-[0-9]+$/', $header_name, $matches)) {
                            foreach ($header_values as $header_value) {
                                $parameters = unserialize(urldecode($header_value));
                                if (count($parameters) === 3) {
                                    if ($parameters[1] === 'User deprecated function') {
                                        // Fire the same deprecation message to allow it to be
                                        // collected by
                                        // \Symfony\Bridge\PhpUnit\DeprecationErrorHandler::collectDeprecations().
                                        @trigger_error((string) $parameters[0], E_USER_DEPRECATED);
                                    }
                                    else {
                                        throw new \Exception($parameters[1] . ': ' . $parameters[0] . "\n" . Error::formatBacktrace([
                                            $parameters[2],
                                        ]));
                                    }
                                }
                                else {
                                    throw new \Exception('Error thrown with the wrong amount of parameters.');
                                }
                            }
                        }
                    }
                    return $response;
                });
            };
        };
    }

}

Members

Title Sort descending Modifiers Object type Summary
TestHttpClientMiddleware::__invoke public function HTTP middleware that replaces the user agent for simpletest requests.

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