Skip to content

Commit 812cb6c

Browse files
author
Jérôme Deuchnord
committed
Fix reviews, add tests against redirections
1 parent 34ef21e commit 812cb6c

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,36 @@ private function collectOnClient(TraceableHttpClient $client): array
171171

172172
private function getCurlCommand(array $trace): ?string
173173
{
174-
$debug = explode("\n", ($trace['info']['debug']));
174+
$debug = explode("\n", $trace['info']['debug']);
175175
$url = $trace['info']['url'];
176176
$command = ['curl', '--compressed'];
177177

178-
$dataArg = null;
178+
$dataArg = [];
179179

180180
if ($json = $trace['options']['json'] ?? null) {
181-
$dataArg = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
181+
$dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
182182
} elseif ($body = $trace['options']['body'] ?? null) {
183183
if (\is_string($body)) {
184-
$dataArg = '--data '.escapeshellarg($body);
184+
$dataArg[] = '--data '.escapeshellarg($body);
185185
} elseif (\is_array($body)) {
186186
foreach ($body as $key => $value) {
187-
$dataArg = '--data '.escapeshellarg("$key=$value");
187+
$dataArg[] = '--data '.escapeshellarg("$key=$value");
188188
}
189189
} else {
190190
return null;
191191
}
192192
}
193193

194+
$dataArg = empty($dataArg) ? null : implode(' ', $dataArg);
195+
194196
foreach ($debug as $line) {
195-
$line = str_replace("\r", '', $line);
197+
$line = substr($line, 0, -1);
198+
199+
if (str_starts_with('< ', $line)) {
200+
// End of the request, beginning of the response. Stop parsing.
201+
break;
202+
}
203+
196204
if ('' === $line || preg_match('/^[*<]|(Host: )/', $line)) {
197205
continue;
198206
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ public function testItGeneratesCurlCommandsAsExpected()
189189
);
190190
}
191191

192+
public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
193+
{
194+
$sut = new HttpClientDataCollector();
195+
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
196+
[
197+
'method' => 'GET',
198+
'url' => 'http://symfony.com/releases.json',
199+
],
200+
]));
201+
$sut->collect(new Request(), new Response());
202+
$collectedData = $sut->getClients();
203+
self::assertCount(1, $collectedData['http_client']['traces']);
204+
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
205+
self::assertEquals("curl \\
206+
--compressed \\
207+
--request GET \\
208+
--url 'https://symfony.com/releases.json' \\
209+
--header 'Accept: */*' \\
210+
--header 'Accept-Encoding: gzip' \\
211+
--header 'User-Agent: Symfony HttpClient/Native'", $curlCommand
212+
);
213+
}
214+
192215
public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
193216
{
194217
$sut = new HttpClientDataCollector();

0 commit comments

Comments
 (0)