From 4503f947ce76a2903aac5b243559cb26a1a1876d Mon Sep 17 00:00:00 2001 From: MatTheCat Date: Mon, 6 Nov 2023 16:54:41 +0100 Subject: [PATCH] [HttpClient][WebProfilerBundle] Do not generate cURL command when files are uploaded --- .../DataCollector/HttpClientDataCollector.php | 7 ++++- .../HttpClientDataCollectorTest.php | 30 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php index 1125aaa58cf7..58399890c265 100644 --- a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php +++ b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpClient\DataCollector; +use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpClient\HttpClientTrait; use Symfony\Component\HttpClient\TraceableHttpClient; use Symfony\Component\HttpFoundation\Request; @@ -199,7 +200,11 @@ private function getCurlCommand(array $trace): ?string if (\is_string($body)) { $dataArg[] = '--data-raw '.$this->escapePayload($body); } elseif (\is_array($body)) { - $body = explode('&', self::normalizeBody($body)); + try { + $body = explode('&', self::normalizeBody($body)); + } catch (TransportException) { + return null; + } foreach ($body as $value) { $dataArg[] = '--data-raw '.$this->escapePayload(urldecode($value)); } diff --git a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php index a168db95e674..df4770f3a1e6 100644 --- a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php +++ b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php @@ -165,8 +165,6 @@ public function testItIsEmptyAfterReset() } /** - * @requires extension openssl - * * @dataProvider provideCurlRequests */ public function testItGeneratesCurlCommandsAsExpected(array $request, string $expectedCurlCommand) @@ -342,9 +340,6 @@ public function __toString(): string } } - /** - * @requires extension openssl - */ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands() { $sut = new HttpClientDataCollector(); @@ -372,9 +367,6 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands() ); } - /** - * @requires extension openssl - */ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType() { $sut = new HttpClientDataCollector(); @@ -394,9 +386,6 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType() self::assertNull($curlCommand); } - /** - * @requires extension openssl - */ public function testItDoesGenerateCurlCommandsForBigData() { $sut = new HttpClientDataCollector(); @@ -416,6 +405,25 @@ public function testItDoesGenerateCurlCommandsForBigData() self::assertNotNull($curlCommand); } + public function testItDoesNotGeneratesCurlCommandsForUploadedFiles() + { + $sut = new HttpClientDataCollector(); + $sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([ + [ + 'method' => 'POST', + 'url' => 'http://localhost:8057/json', + 'options' => [ + 'body' => ['file' => fopen('data://text/plain,', 'r')], + ], + ], + ])); + $sut->lateCollect(); + $collectedData = $sut->getClients(); + self::assertCount(1, $collectedData['http_client']['traces']); + $curlCommand = $collectedData['http_client']['traces'][0]['curlCommand']; + self::assertNull($curlCommand); + } + private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient { $httpClient = new TraceableHttpClient(new NativeHttpClient());