diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index 3ea81aafccc53..f25989e168396 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG * added `EventSourceHttpClient` a Server-Sent events stream implementing the [EventSource specification](https://www.w3.org/TR/eventsource/#eventsource) * added option "extra.curl" to allow setting additional curl options in `CurlHttpClient` * added `RetryableHttpClient` to automatically retry failed HTTP requests. + * added `extra.trace_content` option to `TraceableHttpClient` to prevent it from keeping the content in memory 5.1.0 ----- diff --git a/src/Symfony/Component/HttpClient/Response/TraceableResponse.php b/src/Symfony/Component/HttpClient/Response/TraceableResponse.php index e42b62a6a42af..ea9afa43b1ef1 100644 --- a/src/Symfony/Component/HttpClient/Response/TraceableResponse.php +++ b/src/Symfony/Component/HttpClient/Response/TraceableResponse.php @@ -52,6 +52,10 @@ public function getHeaders(bool $throw = true): array public function getContent(bool $throw = true): string { + if (false === $this->content) { + return $this->response->getContent($throw); + } + $this->content = $this->response->getContent(false); if ($throw) { @@ -63,6 +67,10 @@ public function getContent(bool $throw = true): string public function toArray(bool $throw = true): array { + if (false === $this->content) { + return $this->response->toArray($throw); + } + $this->content = $this->response->toArray(false); if ($throw) { diff --git a/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php index 9b6752f7b62cd..94e557cd893cf 100755 --- a/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php @@ -30,14 +30,14 @@ public function testItTracesRequest() { $httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock(); $httpClient - ->expects($this->once()) + ->expects($this->any()) ->method('request') ->with( 'GET', '/foo/bar', $this->callback(function ($subject) { $onprogress = $subject['on_progress']; - unset($subject['on_progress']); + unset($subject['on_progress'], $subject['extra']); $this->assertEquals(['options1' => 'foo'], $subject); return true; @@ -45,8 +45,11 @@ public function testItTracesRequest() ) ->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse('hello'))) ; + $sut = new TraceableHttpClient($httpClient); + $sut->request('GET', '/foo/bar', ['options1' => 'foo'])->getContent(); + $this->assertCount(1, $tracedRequests = $sut->getTracedRequests()); $actualTracedRequest = $tracedRequests[0]; $this->assertEquals([ @@ -56,6 +59,18 @@ public function testItTracesRequest() 'info' => [], 'content' => 'hello', ], $actualTracedRequest); + + $sut->request('GET', '/foo/bar', ['options1' => 'foo', 'extra' => ['trace_content' => false]])->getContent(); + + $this->assertCount(2, $tracedRequests = $sut->getTracedRequests()); + $actualTracedRequest = $tracedRequests[1]; + $this->assertEquals([ + 'method' => 'GET', + 'url' => '/foo/bar', + 'options' => ['options1' => 'foo', 'extra' => ['trace_content' => false]], + 'info' => [], + 'content' => null, + ], $actualTracedRequest); } public function testItCollectsInfoOnRealRequest() diff --git a/src/Symfony/Component/HttpClient/TraceableHttpClient.php b/src/Symfony/Component/HttpClient/TraceableHttpClient.php index b70c544a66b6f..a061fef7ea80c 100644 --- a/src/Symfony/Component/HttpClient/TraceableHttpClient.php +++ b/src/Symfony/Component/HttpClient/TraceableHttpClient.php @@ -49,6 +49,11 @@ public function request(string $method, string $url, array $options = []): Respo ]; $onProgress = $options['on_progress'] ?? null; + if (false === ($options['extra']['trace_content'] ?? true)) { + unset($content); + $content = false; + } + $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) { $traceInfo = $info;