Skip to content

Commit db78877

Browse files
committed
[HttpClient] Add resolve option to Copy as Curl
1 parent f7d41ac commit db78877

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ private function getCurlCommand(array $trace): ?string
175175
$url = $trace['url'];
176176
$command = ['curl', '--compressed'];
177177

178+
if (isset($trace['options']['resolve'])) {
179+
$port = parse_url($url, \PHP_URL_PORT) ?: (str_starts_with('http:', $url) ? 80 : 443);
180+
foreach ($trace['options']['resolve'] as $host => $ip) {
181+
$command[] = '--resolve '.escapeshellarg("$host:$port:$ip");
182+
}
183+
}
184+
178185
$dataArg = [];
179186

180187
if ($json = $trace['options']['json'] ?? null) {

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

100644100755
+19
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ public function provideCurlRequests(): iterable
194194
--url %1$shttp://localhost:8057/json%1$s \\
195195
--header %1$sAccept: */*%1$s \\
196196
--header %1$sAccept-Encoding: gzip%1$s \\
197+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
198+
];
199+
yield 'GET with resolve' => [
200+
[
201+
'method' => 'GET',
202+
'url' => 'http://localhost:8057/json',
203+
'options' => [
204+
'resolve' => [
205+
'localhost' => '127.0.0.1',
206+
],
207+
],
208+
],
209+
'curl \\
210+
--compressed \\
211+
--resolve %1$slocalhost:8057:127.0.0.1%1$s \\
212+
--request GET \\
213+
--url %1$shttp://localhost:8057/json%1$s \\
214+
--header %1$sAccept: */*%1$s \\
215+
--header %1$sAccept-Encoding: gzip%1$s \\
197216
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
198217
];
199218
yield 'POST with json' => [

0 commit comments

Comments
 (0)