-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Add resolve to copy as Curl #45729
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 \\ | ||
--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', | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -199,20 +298,24 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands() | |
{ | ||
$sut = new HttpClientDataCollector(); | ||
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([ | ||
[ | ||
'method' => 'GET', | ||
'url' => 'http://symfony.com/releases.json', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case don't need to be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
); | ||
|
@@ -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']); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.