Skip to content

Commit

Permalink
[HttpClient] make response stream functionality consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored and nicolas-grekas committed Feb 3, 2020
1 parent 5da9cf3 commit 64f9111
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,4 +92,40 @@ public function testNonBlockingStream()
$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));
}
}

0 comments on commit 64f9111

Please sign in to comment.