8000 minor #45729 [HttpClient] Add resolve to copy as Curl (HypeMC) · symfony/symfony@9a29631 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a29631

Browse files
minor #45729 [HttpClient] Add resolve to copy as Curl (HypeMC)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [HttpClient] Add resolve to copy as Curl | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Adds the resolve option to the generated Curl command. Commits ------- 5f2d584 [HttpClient] Add resolve to copy as Curl
2 parents c758bff + 5f2d584 commit 9a29631

File tree

2 files changed

+135
-23
lines changed

2 files changed

+135
-23
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

Lines changed: 126 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,127 @@ public function testItIsEmptyAfterReset()
168168

169169
/**
170170
* @requires extension openssl
171+
* @dataProvider provideCurlRequests
171172
*/
172-
public function testItGeneratesCurlCommandsAsExpected()
173+
public function testItGeneratesCurlCommandsAsExpected(array $request, string $expectedCurlCommand)
173174
{
174175
$sut = new HttpClientDataCollector();
175-
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
176-
[
177-
'method' => 'GET',
178-
'url' => 'https://symfony.com/releases.json',
179-
],
180-
]));
176+
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([$request]));
181177
$sut->collect(new Request(), new Response());
182178
$collectedData = $sut->getClients();
183179
self::assertCount(1, $collectedData['http_client']['traces']);
184180
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
185-
self::assertEquals(sprintf('curl \\
181+
self::assertEquals(sprintf($expectedCurlCommand, '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand);
182+
}
183+
184+
public function provideCurlRequests(): iterable
185+
{
186+
yield 'GET' => [
187+
[
188+
'method' => 'GET',
189+
'url' => 'http://localhost:8057/json',
190+
],
191+
'curl \\
186192
--compressed \\
187193
--request GET \\
188-
--url %1$shttps://symfony.com/releases.json%1$s \\
194+
--url %1$shttp://localhost:8057/json%1$s \\
189195
--header %1$sAccept: */*%1$s \\
190196
--header %1$sAccept-Encoding: gzip%1$s \\
191-
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s', '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand
192-
);
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 \\
217+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
218+
];
219+
yield 'POST with string body' => [
220+
[
221+
'method' => 'POST',
222+
'url' => 'http://localhost:8057/json',
223+
'options' => [
224+
'body' => 'foobarbaz',
225+
],
226+
],
227+
'curl \\
228+
--compressed \\
229+
--request POST \\
230+
--url %1$shttp://localhost:8057/json%1$s \\
231+
--header %1$sAccept: */*%1$s \\
232+
--header %1$sContent-Length: 9%1$s \\
233+
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
234+
--header %1$sAccept-Encoding: gzip%1$s \\
235+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
236+
--data %1$sfoobarbaz%1$s',
237+
];
238+
yield 'POST with array body' => [
239+
[
240+
'method' => 'POST',
241+
'url' => 'http://localhost:8057/json',
242+
'options' => [
243+
'body' => [
244+
'foo' => 'fooval',
245+
'bar' => 'barval',
246+
'baz' => 'bazval',
247+
],
248+
],
249+
],
250+
'curl \\
251+
--compressed \\
252+
--request POST \\
253+
--url %1$shttp://localhost:8057/json%1$s \\
254+
--header %1$sAccept: */*%1$s \\
255+
--header %1$sContent-Length: 32%1$s \\
256+
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
257+
--header %1$sAccept-Encoding: gzip%1$s \\
258+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
259+
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s',
260+
];
261+
262+
// escapeshellarg on Windows replaces double quotes with spaces
263+
if ('\\' !== \DIRECTORY_SEPARATOR) {
264+
yield 'POST with json' => [
265+
[
266+
'method' => 'POST',
267+
'url' => 'http://localhost:8057/json',
268+
'options' => [
269+
'json' => [
270+
'foo' => [
271+
'bar' => 'baz',
272+
],
273+
],
274+
],
275+
],
276+
'curl \\
277+
--compressed \\
278+
--request POST \\
279+
--url %1$shttp://localhost:8057/json%1$s \\
280+
--header %1$sContent-Type: application/json%1$s \\
281+
--header %1$sAccept: */*%1$s \\
282+
--header %1$sContent-Length: 21%1$s \\
283+
--header %1$sAccept-Encoding: gzip%1$s \\
284+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
285+
--data %1$s{
286+
"foo": {
287+
"bar": "baz"
288+
}
289+
}%1$s',
290+
];
291+
}
193292
}
194293

195294
/**
@@ -199,20 +298,24 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
199298
{
200299
$sut = new HttpClientDataCollector();
201300
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
202-
[
203-
'method' => 'GET',
204-
'url' => 'http://symfony.com/releases.json',
301+
[
302+
'method' => 'GET',
303+
'url' => 'http://localhost:8057/301',
304+
'options' => [
305+
'auth_basic' => 'foo:bar',
205306
],
206-
]));
307+
],
308+
]));
207309
$sut->collect(new Request(), new Response());
208310
$collectedData = $sut->getClients();
209311
self::assertCount(1, $collectedData['http_client']['traces']);
210312
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
211313
self::assertEquals(sprintf('curl \\
212314
--compressed \\
213315
--request GET \\
214-
--url %1$shttp://symfony.com/releases.json%1$s \\
316+
--url %1$shttp://localhost:8057/301%1$s \\
215317
--header %1$sAccept: */*%1$s \\
318+
--header %1$sAuthorization: Basic Zm9vOmJhcg==%1$s \\
216319
--header %1$sAccept-Encoding: gzip%1$s \\
217320
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s', '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand
218321
);
@@ -225,14 +328,14 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
225328
{
226329
$sut = new HttpClientDataCollector();
227330
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
228-
[
229-
'method' => 'GET',
230-
'url' => 'https://symfony.com/releases.json',
231-
'options' => [
232-
'body' => static fn (int $size): string => '',
233-
],
331+
[
332+
'method' => 'GET',
333+
'url' => 'http://localhost:8057/json',
334+
'options' => [
335+
'body' => static fn (int $size): string => '',
234336
],
235-
]));
337+
],
338+
]));
236339
$sut->collect(new Request(), new Response());
237340
$collectedData = $sut->getClients();
238341
self::assertCount(1, $collectedData['http_client']['traces']);

0 commit comments

Comments
 (0)
0