Skip to content

Commit

Permalink
[HttpClient] Improve Copy as Curl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Mar 13, 2022
1 parent 791ee7d commit 9b2587e
Showing 1 changed file with 103 additions and 24 deletions.
127 changes: 103 additions & 24 deletions src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,107 @@ 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 '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,19 +278,19 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'http://symfony.com/releases.json',
],
]));
[
'method' => 'GET',
'url' => 'http://localhost:8057/json',
],
]));
$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/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
Expand All @@ -225,14 +304,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

0 comments on commit 9b2587e

Please sign in to comment.