8000 [HttpClient] Improve Copy as Curl tests · symfony/symfony@6fe7083 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fe7083

Browse files
committed
[HttpClient] Improve Copy as Curl tests
1 parent 791ee7d commit 6fe7083

File tree

1 file changed

+106
-23
lines changed

1 file changed

+106
-23
lines changed

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

100755100644
Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,107 @@ 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 'POST with string body' => [
200+
[
201+
'method' => 'POST',
202+
'url' => 'http://localhost:8057/json',
203+
'options' => [
204+
'body' => 'foobarbaz',
205+
],
206+
],
207+
'curl \\
208+
--compressed \\
209+
--request POST \\
210+
--url %1$shttp://localhost:8057/json%1$s \\
211+
--header %1$sAccept: */*%1$s \\
212+
--header %1$sContent-Length: 9%1$s \\
213+
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
214+
--header %1$sAccept-Encoding: gzip%1$s \\
215+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
216+
--data %1$sfoobarbaz%1$s',
217+
];
218+
yield 'POST with array body' => [
219+
[
220+
'method' => 'POST',
221+
'url' => 'http://localhost:8057/json',
222+
'options' => [
223+
'body' => [
224+
'foo' => 'fooval',
225+
'bar' => 'barval',
226+
'baz' => 'bazval',
227+
],
228+
],
229+
],
230+
'curl \\
231+
--compressed \\
232+
--request POST \\
233+
--url %1$shttp://localhost:8057/json%1$s \\
234+
--header %1$sAccept: */*%1$s \\
235+
--header %1$sContent-Length: 32%1$s \\
236+
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
237+
--header %1$sAccept-Encoding: gzip%1$s \\
238+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
239+
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s',
240+
];
241+
242+
// escapeshellarg on Windows replaces double quotes with spaces
243+
if ('\\' !== \DIRECTORY_SEPARATOR) {
244+
yield 'POST with json' => [
245+
[
246+
'method' => 'POST',
247+
'url' => 'http://localhost:8057/json',
248+
'options' => [
249+
'json' => [
250+
'foo' => [
251+
'bar' => 'baz',
252+
],
253+
],
254+
],
255+
],
256+
'curl \\
257+
--compressed \\
258+
--request POST \\
259+
--url %1$shttp://localhost:8057/json%1$s \\
260+
--header %1$sContent-Type: application/json%1$s \\
261+
--header %1$sAccept: */*%1$s \\
262+
--header %1$sContent-Length: 21%1$s \\
263+
--header %1$sAccept-Encoding: gzip%1$s \\
264+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
265+
--data %1$s{
266+
"foo": {
267+
"bar": "baz"
268+
}
269+
}%1$s',
270+
];
271+
}
193272
}
194273

195274
/**
@@ -199,20 +278,24 @@ public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
199278
{
200279
$sut = new HttpClientDataCollector();
201280
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
202-
[
203-
'method' => 'GET',
204-
'url' => 'http://symfony.com/releases.json',
281+
[
282+
'method' => 'GET',
283+
'url' => 'http://localhost:8057/301',
284+
'options' => [
285+
'auth_basic' => 'foo:bar',
205286
],
206-
]));
287+
],
288+
]));
207289
$sut->collect(new Request(), new Response());
208290
$collectedData = $sut->getClients();
209291
self::assertCount(1, $collectedData['http_client']['traces']);
210292
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
211293
self::assertEquals(sprintf('curl \\
212294
--compressed \\
213295
--request GET \\
214-
--url %1$shttp://symfony.com/releases.json%1$s \\
296+
--url %1$shttp://localhost:8057/301%1$s \\
215297
--header %1$sAccept: */*%1$s \\
298+
--header %1$sAuthorization: Basic Zm9vOmJhcg==%1$s \\
216299
--header %1$sAccept-Encoding: gzip%1$s \\
217300
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s', '\\' === \DIRECTORY_SEPARATOR ? '"' : "'"), $curlCommand
218301
);
@@ -225,14 +308,14 @@ public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
225308
{
226309
$sut = new HttpClientDataCollector();
227310
$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-
],
311+
[
312+
'method' => 'GET',
313+
'url' => 'http://localhost:8057/json',
314+
'options' => [
315+
'body' => static fn (int $size): string => '',
234316
],
235-
]));
317+
],
318+
]));
236319
$sut->collect(new Request(), new Response());
237320
$collectedData = $sut->getClients();
238321
self::assertCount(1, $collectedData['http_client']['traces']);

0 commit comments

Comments
 (0)
0