From 88b1b8fd134fa2aa90d6b16517e112dd5bccffae Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 13 Mar 2022 15:59:39 +0100 Subject: [PATCH] [HttpClient] Add resolve option to Copy as Curl --- .../DataCollector/HttpClientDataCollector.php | 9 +++++++++ .../HttpClientDataCollectorTest.php | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+) mode change 100644 => 100755 src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php diff --git a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php index db158103f9156..6d80d942b3cee 100644 --- a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php +++ b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php @@ -175,6 +175,15 @@ private function getCurlCommand(array $trace): ?string $url = $trace['url']; $command = ['curl', '--compressed']; + if (isset($trace['options']['resolve'])) { + $port = parse_url($url, \PHP_URL_PORT) ?: (str_starts_with('http:', $url) ? 80 : 443); + foreach ($trace['options']['resolve'] as $host => $ip) { + if (null !== $ip) { + $command[] = '--resolve '.escapeshellarg("$host:$port:$ip"); + } + } + } + $dataArg = []; if ($json = $trace['options']['json'] ?? null) { diff --git a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php old mode 100644 new mode 100755 index b0c9295aa39a6..7bc3d62bdd975 --- a/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php +++ b/src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php @@ -194,6 +194,26 @@ public function provideCurlRequests(): iterable --url %1$shttp://localhost:8057/json%1$s \\ --header %1$sAccept: */*%1$s \\ --header %1$sAccept-Encoding: gzip%1$s \\ + --header %1$sUser-Agent: Symfony HttpClient/Native%1$s', + ]; + yield 'GET with resolve' => [ + [ + 'method' => 'GET', + 'url' => 'http://localhost:8057/json', + 'options' => [ + 'resolve' => [ + 'localhost' => '127.0.0.1', + 'example.com' => null, + ], + ], + ], + 'curl \\ + --compressed \\ + --resolve %1$slocalhost:8057:127.0.0.1%1$s \\ + --request GET \\ + --url %1$shttp://localhost:8057/json%1$s \\ + --header %1$sAccept: */*%1$s \\ + --header %1$sAccept-Encoding: gzip%1$s \\ --header %1$sUser-Agent: Symfony HttpClient/Native%1$s', ]; yield 'POST with string body' => [