8000 [HttpClient] Add resolve option to Copy as Curl · symfony/symfony@b0a57a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0a57a3

Browse files
committed
[HttpClient] Add resolve option to Copy as Curl
1 parent 6fe7083 commit b0a57a3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ 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+
if (null !== $ip) {
182+
$command[] = '--resolve '.escapeshellarg("$host:$port:$ip");
183+
}
184+
}
185+
}
186+
178187
$dataArg = [];
179188

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

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

100644100755
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ 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+
'example.com' => null,
207+
],
208+
],
209+
],
210+
'curl \\
211+
--compressed \\
212+
--resolve %1$slocalhost:8057:127.0.0.1%1$s \\
213+
--request GET \\
214+
--url %1$shttp://localhost:8057/json%1$s \\
215+
--header %1$sAccept: */*%1$s \\
216+
--header %1$sAccept-Encoding: gzip%1$s \\
197217
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
198218
];
199219
yield 'POST with string body' => [

0 commit comments

Comments
 (0)
0