[HttpClient] Fix TraceableHttpClient::stream that not works

This commit is contained in:
Laurent VOULLEMIER 2020-03-25 22:29:06 +01:00 committed by Fabien Potencier
parent 4dabd00ecd
commit 575f0408df
2 changed files with 19 additions and 3 deletions

View File

@ -13,9 +13,11 @@ namespace Symfony\Component\HttpClient\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
class TraceableHttpClientTest extends TestCase
{
@ -80,4 +82,18 @@ class TraceableHttpClientTest extends TestCase
$sut->reset();
$this->assertCount(0, $sut->getTracedRequests());
}
public function testStream()
{
TestHttpServer::start();
$sut = new TraceableHttpClient(new NativeHttpClient());
$chunked = $sut->request('GET', 'http://localhost:8057/chunked');
$chunks = [];
foreach ($sut->stream($chunked) as $response) {
$chunks[] = $response->getContent();
}
$this->assertGreaterThan(1, \count($chunks));
$this->assertSame('Symfony is awesome!', implode('', $chunks));
}
}

View File

@ -67,18 +67,18 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
if ($responses instanceof TraceableResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}
return $this->client->stream(\Closure::bind(static function () use ($responses) {
foreach ($responses as $k => $r) {
if (!$r instanceof TraceableResponse) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
}
yield $k => $r->response;
}
}, null, TraceableResponse::class), $timeout);
}, null, TraceableResponse::class)(), $timeout);
}
public function getTracedRequests(): array