From 64f91116866df8601063780fcc91539ee4445ea9 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 3 Feb 2020 08:37:34 -0500 Subject: [PATCH] [HttpClient] make response stream functionality consistent --- .../HttpClient/Response/StreamWrapper.php | 2 +- .../HttpClient/Tests/HttpClientTestCase.php | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/Response/StreamWrapper.php b/src/Symfony/Component/HttpClient/Response/StreamWrapper.php index 464c4e567f..0755c22876 100644 --- a/src/Symfony/Component/HttpClient/Response/StreamWrapper.php +++ b/src/Symfony/Component/HttpClient/Response/StreamWrapper.php @@ -49,7 +49,7 @@ class StreamWrapper */ public static function createResource(ResponseInterface $response, HttpClientInterface $client = null) { - if (null === $client && \is_callable([$response, 'toStream']) && isset(class_uses($response)[ResponseTrait::class])) { + if (\is_callable([$response, 'toStream']) && isset(class_uses($response)[ResponseTrait::class])) { $stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2); if ($response !== ($stack[1]['object'] ?? null)) { diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php index 29171969b4..5017b2e3c4 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpClient\Tests; use Symfony\Component\HttpClient\Exception\ClientException; +use Symfony\Component\HttpClient\Response\StreamWrapper; use Symfony\Contracts\HttpClient\Test\HttpClientTestCase as BaseHttpClientTestCase; abstract class HttpClientTestCase extends BaseHttpClientTestCase @@ -91,4 +92,40 @@ abstract class HttpClientTestCase extends BaseHttpClientTestCase $this->assertSame('', fread($stream, 8192)); $this->assertTrue(feof($stream)); } + + public function testResponseStreamRewind() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://localhost:8057/103'); + + $stream = $response->toStream(); + + $this->assertSame('Here the body', stream_get_contents($stream)); + rewind($stream); + $this->assertSame('Here the body', stream_get_contents($stream)); + } + + public function testStreamWrapperStreamRewind() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://localhost:8057/103'); + + $stream = StreamWrapper::createResource($response); + + $this->assertSame('Here the body', stream_get_contents($stream)); + rewind($stream); + $this->assertSame('Here the body', stream_get_contents($stream)); + } + + public function testStreamWrapperWithClientStreamRewind() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://localhost:8057/103'); + + $stream = StreamWrapper::createResource($response, $client); + + $this->assertSame('Here the body', stream_get_contents($stream)); + rewind($stream); + $this->assertSame('Here the body', stream_get_contents($stream)); + } }