diff --git a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php index 96bbe695b92ea..903da4911b0a0 100644 --- a/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php +++ b/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php @@ -131,8 +131,9 @@ private function collectOnClient(TraceableHttpClient $client): array } $debugInfo = array_diff_key($info, $baseInfo); - $info = array_diff_key($info, $debugInfo) + ['debug_info' => $debugInfo]; - unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient + $responseContent = isset($trace['response_content']) ? $trace['response_content'] : null; + $info = array_diff_key($info, $debugInfo) + ['response_content' => $responseContent, 'debug_info' => $debugInfo]; + unset($traces[$i]['info'], $traces[$i]['response_content']); // break PHP reference used by TraceableHttpClient $traces[$i]['info'] = $this->cloneVar($info); $traces[$i]['options'] = $this->cloneVar($trace['options']); } diff --git a/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php index 949d8afcff85a..39b983cc16790 100755 --- a/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php @@ -36,7 +36,7 @@ public function testItTracesRequest() return true; }) ) - ->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse())) + ->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse('{"foo": "bar"}'))) ; $sut = new TraceableHttpClient($httpClient); $sut->request('GET', '/foo/bar', ['options1' => 'foo']); @@ -47,6 +47,7 @@ public function testItTracesRequest() 'url' => '/foo/bar', 'options' => ['options1' => 'foo'], 'info' => [], + 'response_content' => ['foo' => 'bar'], ], $actualTracedRequest); } @@ -58,6 +59,7 @@ public function testItCollectsInfoOnRealRequest() $actualTracedRequest = $tracedRequests[0]; $this->assertSame('GET', $actualTracedRequest['info']['http_method']); $this->assertSame('http://localhost:8057/', $actualTracedRequest['info']['url']); + $this->assertNull($actualTracedRequest['response_content']); } public function testItExecutesOnProgressOption() diff --git a/src/Symfony/Component/HttpClient/TraceableHttpClient.php b/src/Symfony/Component/HttpClient/TraceableHttpClient.php index d60d0849cd95e..e9dd7a8a4a7ff 100644 --- a/src/Symfony/Component/HttpClient/TraceableHttpClient.php +++ b/src/Symfony/Component/HttpClient/TraceableHttpClient.php @@ -35,11 +35,13 @@ public function __construct(HttpClientInterface $client) public function request(string $method, string $url, array $options = []): ResponseInterface { $traceInfo = []; + $responseContent = null; $this->tracedRequests[] = [ 'method' => $method, 'url' => $url, 'options' => $options, 'info' => &$traceInfo, + 'response_content' => &$responseContent, ]; $onProgress = $options['on_progress'] ?? null; @@ -51,7 +53,15 @@ public function request(string $method, string $url, array $options = []): Respo } }; - return $this->client->request($method, $url, $options); + $response = $this->client->request($method, $url, $options); + + // Try to convert response to array if possible + try { + $responseContent = $response->toArray(false); + } catch (\Throwable $e) { + } + + return $response; } /**