Skip to content

Commit 9d6b34a

Browse files
bug #35573 [HttpClient] make response stream functionality consistent (kbond)
This PR was squashed before being merged into the 5.1-dev branch. Discussion ---------- [HttpClient] make response stream functionality consistent | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a There are three ways of creating a stream from a response: 1. Calling `$response->toStream()` (if the response supports this) 2. Calling `StreamWrapper::createResource($response)` 3. Calling `StreamWrapper::createResource($response, $httpClient)` (note the second argument) Currently, the 3rd method creates a stream that is not rewindable (the other two are). The first commit adds tests showing the inconsistencies (1 test fails). The second commit is a fix to make the 3 ways consistent. See https://twitter.com/nicolasgrekas/status/1224047079422599168 for reference. Commits ------- 64f9111686 [HttpClient] make response stream functionality consistent
2 parents 1ae661c + 3979fb6 commit 9d6b34a

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)