Skip to content

Commit 3979fb6

Browse files
kbondnicolas-grekas
authored andcommitted
[HttpClient] make response stream functionality consistent
1 parent ffcca84 commit 3979fb6

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Response/StreamWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class StreamWrapper
4949
*/
5050
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
5151
{
52-
if (null === $client && \is_callable([$response, 'toStream']) && isset(class_uses($response)[ResponseTrait::class])) {
52+
if (\is_callable([$response, 'toStream']) && isset(class_uses($response)[ResponseTrait::class])) {
5353
$stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2);
5454

5555
if ($response !== ($stack[1]['object'] ?? null)) {

Tests/HttpClientTestCase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpClient\Tests;
1313

1414
use Symfony\Component\HttpClient\Exception\ClientException;
15+
use Symfony\Component\HttpClient\Response\StreamWrapper;
1516
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase as BaseHttpClientTestCase;
1617

1718
abstract class HttpClientTestCase extends BaseHttpClientTestCase
@@ -91,4 +92,40 @@ public function testNonBlockingStream()
9192
$this->assertSame('', fread($stream, 8192));
9293
$this->assertTrue(feof($stream));
9394
}
95+
96+
public function testResponseStreamRewind()
97+
{
98+
$client = $this->getHttpClient(__FUNCTION__);
99+
$response = $client->request('GET', 'http://localhost:8057/103');
100+
101+
$stream = $response->toStream();
102+
103+
$this->assertSame('Here the body', stream_get_contents($stream));
104+
rewind($stream);
105+
$this->assertSame('Here the body', stream_get_contents($stream));
106+
}
107+
108+
public function testStreamWrapperStreamRewind()
109+
{
110+
$client = $this->getHttpClient(__FUNCTION__);
111+
$response = $client->request('GET', 'http://localhost:8057/103');
112+
113+
$stream = StreamWrapper::createResource($response);
114+
115+
$this->assertSame('Here the body', stream_get_contents($stream));
116+
rewind($stream);
117+
$this->assertSame('Here the body', stream_get_contents($stream));
118+
}
119+
120+
public function testStreamWrapperWithClientStreamRewind()
121+
{
122+
$client = $this->getHttpClient(__FUNCTION__);
123+
$response = $client->request('GET', 'http://localhost:8057/103');
124+
125+
$stream = StreamWrapper::createResource($response, $client);
126+
127+
$this->assertSame('Here the body', stream_get_contents($stream));
128+
rewind($stream);
129+
$this->assertSame('Here the body', stream_get_contents($stream));
130+
}
94131
}

0 commit comments

Comments
 (0)