Skip to content

Commit 03cc221

Browse files
committed
[HttpClient][WebProfilerBundle] Catch errors when encoding body for curl command line
1 parent 17b7a59 commit 03cc221

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ private function getCurlCommand(array $trace): ?string
194194
$dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
195195
} elseif ($body = $trace['options']['body'] ?? null) {
196196
if (\is_string($body)) {
197-
$dataArg[] = '--data '.escapeshellarg($body);
197+
try {
198+
$dataArg[] = '--data '.escapeshellarg($body);
199+
} catch (\ValueError $e) {
200+
return null;
201+
}
198202
} elseif (\is_array($body)) {
199203
foreach ($body as $key => $value) {
200204
$dataArg[] = '--data '.escapeshellarg("$key=$value");

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,28 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
343343
self::assertNull($curlCommand);
344344
}
345345

346+
/**
347+
* @requires extension openssl
348+
*/
349+
public function testItDoesNotGeneratesCurlCommandsForNotEncodableBody()
350+
{
351+
$sut = new HttpClientDataCollector();
352+
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
353+
[
354+
'method' => 'POST',
355+
'url' => 'http://localhost:8057/json',
356+
'options' => [
357+
'body' => "\0",
358+
],
359+
],
360+
]));
361+
$sut->collect(new Request(), new Response());
362+
$collectedData = $sut->getClients();
363+
self::assertCount(1, $collectedData['http_client']['traces']);
364+
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
365+
self::assertNull($curlCommand);
366+
}
367+
346368
private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient
347369
{
348370
$httpClient = new TraceableHttpClient(new NativeHttpClient());

0 commit comments

Comments
 (0)