Skip to content

[HttpClient] Copy as curl fixes #46583

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

Merged
merged 1 commit into from
Jun 9, 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 @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpClient\DataCollector;

use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -23,6 +24,8 @@
*/
final class HttpClientDataCollector extends DataCollector implements LateDataCollectorInterface
{
use HttpClientTrait;

/**
* @var TraceableHttpClient[]
*/
Expand Down Expand Up @@ -176,7 +179,7 @@ private function getCurlCommand(array $trace): ?string
}

$debug = explode("\n", $trace['info']['debug']);
$url = $trace['url'];
$url = self::mergeQueryString($trace['url'], $trace['options']['query'] ?? [], true);
$command = ['curl', '--compressed'];

if (isset($trace['options']['resolve'])) {
Expand All @@ -196,8 +199,9 @@ private function getCurlCommand(array $trace): ?string
if (\is_string($body)) {
$dataArg[] = '--data '.escapeshellarg($body);
} elseif (\is_array($body)) {
foreach ($body as $key => $value) {
$dataArg[] = '--data '.escapeshellarg("$key=$value");
$body = explode('&', self::normalizeBody($body));
foreach ($body as $value) {
$dataArg[] = '--data '.escapeshellarg(urldecode($value));
}
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ public function provideCurlRequests(): iterable
'foo' => 'fooval',
'bar' => 'barval',
'baz' => 'bazval',
'foobar' => [
'baz' => 'bazval',
'qux' => 'quxval',
],
'bazqux' => ['bazquxval1', 'bazquxval2'],
'object' => (object) [
'fooprop' => 'foopropval',
'barprop' => 'barpropval',
],
'tostring' => new class() {
public function __toString(): string
{
return 'tostringval';
}
},
],
],
],
Expand All @@ -253,14 +268,37 @@ public function provideCurlRequests(): iterable
--url %1$shttp://localhost:8057/json%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
--header %1$sContent-Length: 32%1$s \\
--header %1$sContent-Length: 211%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',
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s --data %1$sfoobar[baz]=bazval%1$s --data %1$sfoobar[qux]=quxval%1$s --data %1$sbazqux[0]=bazquxval1%1$s --data %1$sbazqux[1]=bazquxval2%1$s --data %1$sobject[fooprop]=foopropval%1$s --data %1$sobject[barprop]=barpropval%1$s --data %1$stostring=tostringval%1$s',
];

// escapeshellarg on Windows replaces double quotes with spaces
// escapeshellarg on Windows replaces double quotes & percent signs with spaces
if ('\\' !== \DIRECTORY_SEPARATOR) {
yield 'GET with query' => [
[
'method' => 'GET',
'url' => 'http://localhost:8057/?foo=fooval&bar=barval',
'options' => [
'query' => [
'bar' => 'newbarval',
'foobar' => [
'baz' => 'bazval',
'qux' => 'quxval',
],
'bazqux' => ['bazquxval1', 'bazquxval2'],
],
],
],
'curl \\
--compressed \\
--request GET \\
--url %1$shttp://localhost:8057/?foo=fooval&bar=newbarval&foobar%%5Bbaz%%5D=bazval&foobar%%5Bqux%%5D=quxval&bazqux%%5B0%%5D=bazquxval1&bazqux%%5B1%%5D=bazquxval2%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 json' => [
[
'method' => 'POST',
Expand Down