Skip to content

Commit

Permalink
[HttpClient] Add resolve option to Copy as Curl
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Mar 15, 2022
1 parent 9b2587e commit 88b1b8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 88b1b8f

Please sign in to comment.