Skip to content

Commit 0e38d39

Browse files
committed
[HttpClient] Fix copy as curl for arrays with resources & unreachable host
1 parent a67de20 commit 0e38d39

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

DataCollector/HttpClientDataCollector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,14 @@ private function getCurlCommand(array $trace): ?string
203203
$dataArg[] = '--data-raw '.$this->escapePayload($body);
204204
} elseif (\is_array($body)) {
205205
try {
206-
$body = explode('&', self::normalizeBody($body));
206+
$body = self::normalizeBody($body);
207207
} catch (TransportException) {
208208
return null;
209209
}
210-
foreach ($body as $value) {
210+
if (!\is_string($body)) {
211+
return null;
212+
}
213+
foreach (explode('&', $body) as $value) {
211214
$dataArg[] = '--data-raw '.$this->escapePayload(urldecode($value));
212215
}
213216
} else {

Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\HttpClient\Tests\DataCollector;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\CurlHttpClient;
1516
use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector;
17+
use Symfony\Component\HttpClient\Exception\TransportException;
1618
use Symfony\Component\HttpClient\MockHttpClient;
1719
use Symfony\Component\HttpClient\NativeHttpClient;
1820
use Symfony\Component\HttpClient\TraceableHttpClient;
@@ -427,6 +429,27 @@ public function testItDoesNotGeneratesCurlCommandsForUploadedFiles()
427429
self::assertNull($curlCommand);
428430
}
429431

432+
/**
433+
* @requires extension curl
434+
*/
435+
public function testGeneratingCurlCommandForArraysWithResourcesAndUnreachableHost()
436+
{
437+
$httpClient = new TraceableHttpClient(new CurlHttpClient());
438+
try {
439+
$httpClient->request('POST', 'http://localhast:8057/', [
440+
'body' => ['file' => fopen('data://text/plain,', 'r')],
441+
]);
442+
} catch (TransportException) {
443+
}
444+
$sut = new HttpClientDataCollector();
445+
$sut->registerClient('http_client', $httpClient);
446+
$sut->lateCollect();
447+
$collectedData = $sut->getClients();
448+
self::assertCount(1, $collectedData['http_client']['traces']);
449+
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
450+
self::assertNull($curlCommand);
451+
}
452+
430453
private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient
431454
{
432455
$httpClient = new TraceableHttpClient(new NativeHttpClient());

0 commit comments

Comments
 (0)