From 12b478efd3e0aa57ec6ef63fe9204507c01490f7 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 10 Feb 2021 17:10:39 -0800 Subject: [PATCH] GuzzleMiddleware, HttpTestTrait - Add 'DEBUG' option If you run the test with DEBUG=1 or DEBUG=2, it will show a list of the HTTP requests. --- CRM/Utils/EchoLogger.php | 30 ++++++++++++++++++++++++++++++ CRM/Utils/GuzzleMiddleware.php | 31 +++++++++++++++++++++++++++++++ Civi/Test/HttpTestTrait.php | 10 +++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 CRM/Utils/EchoLogger.php diff --git a/CRM/Utils/EchoLogger.php b/CRM/Utils/EchoLogger.php new file mode 100644 index 0000000000..c6db7c04b7 --- /dev/null +++ b/CRM/Utils/EchoLogger.php @@ -0,0 +1,30 @@ +getMethod() !== 'GET') { + $cmd .= ' -X ' . escapeshellarg($request->getMethod()); + } + foreach ($request->getHeaders() as $header => $lines) { + foreach ($lines as $line) { + $cmd .= ' -H ' . escapeshellarg("$header: $line"); + } + } + $body = (string) $request->getBody(); + if ($body !== '') { + $cmd .= ' -d ' . escapeshellarg($body); + } + $cmd .= ' ' . escapeshellarg((string) $request->getUri()); + return $cmd; + } + + }; + + return \GuzzleHttp\Middleware::log($logger, $curlFmt); + } + } diff --git a/Civi/Test/HttpTestTrait.php b/Civi/Test/HttpTestTrait.php index 8b28883a66..cdc42c64de 100644 --- a/Civi/Test/HttpTestTrait.php +++ b/Civi/Test/HttpTestTrait.php @@ -3,6 +3,7 @@ namespace Civi\Test; use GuzzleHttp\HandlerStack; +use GuzzleHttp\MessageFormatter; use GuzzleHttp\Middleware; /** @@ -38,9 +39,16 @@ trait HttpTestTrait { */ protected function createGuzzle($options = []) { $handler = HandlerStack::create(); - $handler->push(\CRM_Utils_GuzzleMiddleware::url(), 'civi_url'); + $handler->unshift(\CRM_Utils_GuzzleMiddleware::url(), 'civi_url'); $handler->push(Middleware::history($this->httpHistory), 'history'); + if (getenv('DEBUG') >= 2) { + $handler->push(Middleware::log(new \CRM_Utils_EchoLogger(), new MessageFormatter(MessageFormatter::DEBUG)), 'log'); + } + elseif (getenv('DEBUG') >= 1) { + $handler->push(\CRM_Utils_GuzzleMiddleware::curlLog(new \CRM_Utils_EchoLogger()), 'curl_log'); + } + $defaults = [ 'handler' => $handler, 'base_uri' => 'auto:', -- 2.25.1