Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpClient] Add resolve to copy as Curl #45729

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,127 @@ public function testItIsEmptyAfterReset()

/**
* @requires extension openssl
* @dataProvider provideCurlRequests
*/
public function testItGeneratesCurlCommandsAsExpected()
public function testItGeneratesCurlCommandsAsExpected(array $request, string $expectedCurlCommand)
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'https://symfony.com/releases.json',
],
]));
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([$request]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
self::assertEquals(sprintf('curl \\
self::assertEquals(sprintf($expectedCurlCommand, '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand);
}

public function provideCurlRequests(): iterable
{
yield 'GET' => [
[
'method' => 'GET',
'url' => 'http://localhost:8057/json',
],
'curl \\
--compressed \\
--request GET \\
--url %1$shttps://symfony.com/releases.json%1$s \\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't keep a case using the default port.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN Not sure what to do about this one, I could revert to symfony.com, but as mentioned, I was hoping to avoid making requests to 3rd party services.

--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', '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand
);
--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' => [
[
'method' => 'POST',
'url' => 'http://localhost:8057/json',
'options' => [
'body' => 'foobarbaz',
],
],
'curl \\
--compressed \\
--request POST \\
--url %1$shttp://localhost:8057/json%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sContent-Length: 9%1$s \\
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
--data %1$sfoobarbaz%1$s',
];
yield 'POST with array body' => [
[
'method' => 'POST',
'url' => 'http://localhost:8057/json',
'options' => [
'body' => [
'foo' => 'fooval',
'bar' => 'barval',
'baz' => 'bazval',
],
],
],
'curl \\
--compressed \\
--request POST \\
--url %1$shttp://localhost:8057/json%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sContent-Length: 32%1$s \\
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s',
];

// escapeshellarg on Windows replaces double quotes with spaces
if ('\\' !== \DIRECTORY_SEPARATOR) {
yield 'POST with json' => [
[
'method' => 'POST',
'url' => 'http://localhost:8057/json',
'options' => [
'json' => [
'foo' => [
'bar' => 'baz',
],
],
],
],
'curl \\
--compressed \\
--request POST \\
--url %1$shttp://localhost:8057/json%1$s \\
--header %1$sContent-Type: application/json%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sContent-Length: 21%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
--data %1$s{
"foo": {
"bar": "baz"
}
}%1$s',
];
}
}

/**
Expand All @@ -199,20 +298,24 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'http://symfony.com/releases.json',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case don't need to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN I changed it because the test was making a request to symfony.com, so I though it'd be better if it didn't rely on a 3rd party service. I can revert it if you want.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the records, this test's purpose was to check that the redirections are not followed (in this case http → https), so the returned cURL command corresponds exactly to the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Deuchnord Tnx for the info, I totally missed that. I've updated the test to go to a url that redirects.

[
'method' => 'GET',
'url' => 'http://localhost:8057/301',
'options' => [
'auth_basic' => 'foo:bar',
],
]));
],
]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
self::assertEquals(sprintf('curl \\
--compressed \\
--request GET \\
--url %1$shttp://symfony.com/releases.json%1$s \\
--url %1$shttp://localhost:8057/301%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sAuthorization: Basic Zm9vOmJhcg==%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s', '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand
);
Expand All @@ -225,14 +328,14 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'https://symfony.com/releases.json',
'options' => [
'body' => static fn (int $size): string => '',
],
[
'method' => 'GET',
'url' => 'http://localhost:8057/json',
'options' => [
'body' => static fn (int $size): string => '',
],
]));
],
]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
Expand Down