function BrowserHtmlDebugTrait::getResponseLogHandler

Same name in other branches
  1. 9 core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php \Drupal\Tests\BrowserHtmlDebugTrait::getResponseLogHandler()
  2. 8.9.x core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php \Drupal\Tests\BrowserHtmlDebugTrait::getResponseLogHandler()
  3. 10 core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php \Drupal\Tests\BrowserHtmlDebugTrait::getResponseLogHandler()

Provides a Guzzle middleware handler to log every response received.

Return value

callable The callable handler that will do the logging.

1 call to BrowserHtmlDebugTrait::getResponseLogHandler()
BrowserTestBase::initMink in core/tests/Drupal/Tests/BrowserTestBase.php
Initializes Mink sessions.

File

core/tests/Drupal/Tests/BrowserHtmlDebugTrait.php, line 159

Class

BrowserHtmlDebugTrait
Provides the debug functions for browser tests.

Namespace

Drupal\Tests

Code

protected function getResponseLogHandler() {
    return function (callable $handler) {
        return function (RequestInterface $request, array $options) use ($handler) {
            return $handler($request, $options)->then(function (ResponseInterface $response) use ($request) {
                if ($this->htmlOutputEnabled) {
                    $caller = $this->getTestMethodCaller();
                    $html_output = 'Called from ' . $caller['function'] . ' line ' . $caller['line'];
                    $html_output .= '<hr />' . $request->getMethod() . ' request to: ' . $request->getUri();
                    
                    /** @var \Psr\Http\Message\StreamInterface $stream */
                    $stream = $response->getBody();
                    // Get the response body as a string. The response stream is set
                    // to the sink, which defaults to a readable temp stream but can
                    // be overridden by setting $options['sink'].
                    $body = $stream->isReadable() ? (string) $stream : 'Response is not readable.';
                    // On redirect responses (status code starting with '3') we need
                    // to remove the meta tag that would do a browser refresh. We
                    // don't want to redirect developers away when they look at the
                    // debug output file in their browser.
                    $status_code = (string) $response->getStatusCode();
                    if ($status_code[0] === '3') {
                        $body = preg_replace('#<meta http-equiv="refresh" content=.+/>#', '', $body, 1);
                    }
                    $html_output .= '<hr />' . $body;
                    $html_output .= $this->formatHtmlOutputHeaders($response->getHeaders());
                    $this->htmlOutput($html_output);
                }
                return $response;
            });
        };
    };
}

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