Skip to content

Commit bfb140d

Browse files
committed
move threshold check below content-type check, as it might be more resource intensive (getBody())
whitelist empty response Content-Type by default
1 parent 67de1c1 commit bfb140d

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

config/http-client-global-logger.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,17 @@
114114
|--------------------------------------------------------------------------
115115
|
116116
| Trim response body to a certain length. This is useful when you are logging
117-
| large responses and you don't want to fill up your log files.
117+
| large responses, and you don't want to fill up your log files.
118+
|
119+
| NOTE the leading comma in trim_response_body.content_type_whitelist default value:
120+
| it's there to whitelist empty content types (e.g. when no Content-Type header is set).
118121
*/
119122
'trim_response_body' => [
120123
'enabled' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED', false),
121124
'treshold' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD', 200),
122125
'content_type_whitelist' => explode(',', env(
123126
'HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST',
124-
'application/json'
127+
',application/json'
125128
)),
126129
],
127130
];

src/Listeners/LogResponseReceived.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Facades\Log;
1010
use Illuminate\Support\Str;
1111
use Onlime\LaravelHttpClientGlobalLogger\EventHelper;
12+
use Psr\Http\Message\MessageInterface;
1213
use Saloon\Laravel\Events\SentSaloonRequest;
1314

1415
class LogResponseReceived
@@ -32,20 +33,13 @@ public function handle(ResponseReceived|SentSaloonRequest $event): void
3233
/**
3334
* Trim the response body when it's too long.
3435
*/
35-
private function trimBody(Response $psrResponse): Response
36+
private function trimBody(Response $psrResponse): Response|MessageInterface
3637
{
3738
// Check if trimming is enabled
3839
if (! config('http-client-global-logger.trim_response_body.enabled')) {
3940
return $psrResponse;
4041
}
4142

42-
$treshold = config('http-client-global-logger.trim_response_body.treshold');
43-
44-
// Check if the body size exceeds the treshold
45-
if ($psrResponse->getBody()->getSize() <= $treshold) {
46-
return $psrResponse;
47-
}
48-
4943
// E.g.: application/json; charset=utf-8 => application/json
5044
$contentTypeHeader = Str::of($psrResponse->getHeaderLine('Content-Type'))
5145
->before(';')
@@ -63,8 +57,13 @@ private function trimBody(Response $psrResponse): Response
6357
return $psrResponse;
6458
}
6559

66-
return $psrResponse->withBody(Utils::streamFor(
67-
Str::limit($psrResponse->getBody(), $treshold)
68-
));
60+
$treshold = config('http-client-global-logger.trim_response_body.treshold');
61+
62+
// Check if the body size exceeds the treshold
63+
return ($psrResponse->getBody()->getSize() <= $treshold)
64+
? $psrResponse
65+
: $psrResponse->withBody(Utils::streamFor(
66+
Str::limit($psrResponse->getBody(), $treshold)
67+
));
6968
}
7069
}

tests/HttpClientLoggerTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function setupLogger(): MockInterface
4444
Http::fake()->get('https://example.com');
4545
});
4646

47-
it('can trim the body response', function (array $config, bool $shouldTrim, bool $addCharsetToContentType) {
47+
it('can trim the body response', function (array $config, string $contentType, bool $shouldTrim, bool $addCharsetToContentType) {
4848
config(['http-client-global-logger.trim_response_body' => $config]);
4949

5050
$logger = setupLogger();
@@ -61,7 +61,7 @@ function setupLogger(): MockInterface
6161

6262
Http::fake([
6363
'*' => Http::response('verylongbody', 200, [
64-
'content-type' => 'application/octet-stream'.($addCharsetToContentType ? '; charset=UTF-8' : ''),
64+
'Content-Type' => $contentType.($addCharsetToContentType ? '; charset=UTF-8' : ''),
6565
]),
6666
])->get('https://example.com');
6767
})->with(
@@ -72,6 +72,7 @@ function setupLogger(): MockInterface
7272
'treshold' => 10,
7373
'content_type_whitelist' => ['application/json'],
7474
],
75+
'contentType' => 'application/octet-stream',
7576
'shouldTrim' => false,
7677
],
7778
'below_treshold' => [
@@ -80,6 +81,7 @@ function setupLogger(): MockInterface
8081
'treshold' => 20,
8182
'content_type_whitelist' => ['application/json'],
8283
],
84+
'contentType' => 'application/octet-stream',
8385
'shouldTrim' => false,
8486
],
8587
'content_type_whitelisted' => [
@@ -88,6 +90,7 @@ function setupLogger(): MockInterface
8890
'treshold' => 10,
8991
'content_type_whitelist' => ['application/octet-stream'],
9092
],
93+
'contentType' => 'application/octet-stream',
9194
'shouldTrim' => false,
9295
],
9396
'trim' => [
@@ -96,8 +99,27 @@ function setupLogger(): MockInterface
9699
'treshold' => 10,
97100
'content_type_whitelist' => ['application/json'],
98101
],
102+
'contentType' => 'application/octet-stream',
99103
'shouldTrim' => true,
100104
],
105+
'no_content_type_trim' => [
106+
'config' => [
107+
'enabled' => true,
108+
'treshold' => 10,
109+
'content_type_whitelist' => ['application/octet-stream'],
110+
],
111+
'contentType' => '',
112+
'shouldTrim' => true,
113+
],
114+
'no_content_type_whitelisted' => [
115+
'config' => [
116+
'enabled' => true,
117+
'treshold' => 10,
118+
'content_type_whitelist' => ['', 'application/octet-stream'],
119+
],
120+
'contentType' => '',
121+
'shouldTrim' => false,
122+
],
101123
],
102124
[true, false]
103125
);

0 commit comments

Comments
 (0)