[HttpClient] Fix TraceableHttpClient::stream that not works
This commit is contained in:
parent
4dabd00ecd
commit
575f0408df
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user