diff --git a/README.md b/README.md index 5a55ff7..02b7acf 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ The default logger and filter are specified in the package configuration `logger Http::log($context, $config, $logger, $filter)->get('https://example.com'); // or Http::logWhen($condition, $context, $config, $logger, $filter)->get('https://example.com'); +// or the shorthand syntax which will use the LogAllFilter +Http::logWith($logger)->get('https://example.com'); ``` Note that the logger must implement `HttpLoggerInterface` while the filter must implement `HttpLoggingFilterInterface`. diff --git a/src/LaravelHttpClientLoggerServiceProvider.php b/src/LaravelHttpClientLoggerServiceProvider.php index 9c622ce..e44507f 100644 --- a/src/LaravelHttpClientLoggerServiceProvider.php +++ b/src/LaravelHttpClientLoggerServiceProvider.php @@ -58,6 +58,11 @@ public function packageBooted() return $this; } }); + + PendingRequest::macro('logWith', function (HttpLoggerInterface $logger = null) { + /** @var \Illuminate\Http\Client\PendingRequest $this */ + return $this->withMiddleware((new LoggingMiddleware($logger, new LogAllFilter()))->__invoke()); + }); } /** diff --git a/src/LogAllFilter.php b/src/LogAllFilter.php new file mode 100644 index 0000000..2e1d50d --- /dev/null +++ b/src/LogAllFilter.php @@ -0,0 +1,19 @@ +filterMessage($request); + return $filtered->withUri($this->getUri($request)); } diff --git a/src/NullLogger.php b/src/NullLogger.php new file mode 100644 index 0000000..be3dbec --- /dev/null +++ b/src/NullLogger.php @@ -0,0 +1,19 @@ +assertTrue($logAllFilter->shouldLog( + $this->mock(RequestInterface::class), + $this->mock(ResponseInterface::class), + 123 + )); + } +} diff --git a/tests/MacroTest.php b/tests/MacroTest.php index 484639a..9f8e8fb 100644 --- a/tests/MacroTest.php +++ b/tests/MacroTest.php @@ -9,17 +9,17 @@ public function test_log() // TODO: Implement } - public function test_log_with_context() + public function test_log_parameter_context() { // TODO: Implement } - public function test_log_with_config() + public function test_log_parameter_config() { // TODO: Implement } - public function test_log_with_logger() + public function test_log_parameter_logger() { // TODO: Implement } @@ -63,4 +63,9 @@ public function test_log_when_with_logger() { // TODO: Implement } + + public function test_log_with() + { + // TODO: Implement + } } diff --git a/tests/MessageAccessorTest.php b/tests/MessageAccessorTest.php index f7c80a6..7d5702c 100644 --- a/tests/MessageAccessorTest.php +++ b/tests/MessageAccessorTest.php @@ -147,7 +147,7 @@ public function test_get_content() public function test_filter_message() { - $request = $this->messageAccessor->filter($this->request); + $request = $this->messageAccessor->filterMessage($this->request); // Note that it is required to use double quotes for the Carriage Return (\r) to work and have it on one line to pass on Windows $output = "POST /some-path/secret/should-not-be-removed?test=true&search=foo&filter%5Bfield1%5D=A&filter%5Bfield2%5D=B HTTP/1.1\r\nHost: ********.example.com:9000\r\nAccept: application/json\r\nContent-Type: application/json\r\nAuthorization: ********\r\n\r\n{\"data\":{\"foo\":\"bar\",\"baz\":[{\"field_1\":\"value1\",\"field_2\":\"value2\",\"password\":\"********\",\"secret\":\"this is not for everyone\"}]}}";