[HttpClient] make response stream functionality consistent

This commit is contained in:
Kevin Bond 2020-02-03 08:37:34 -05:00 committed by Nicolas Grekas
parent 5da9cf315f
commit 64f9111686
2 changed files with 38 additions and 1 deletions

View File

@ -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)) {

View File

@ -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));
}
}