Skip to content

Commit

Permalink
[HttpClient] Fix TraceableHttpClient::stream that not works
Browse files Browse the repository at this point in the history
  • Loading branch information
l-vo authored and fabpot committed Mar 27, 2020
1 parent 4dabd00 commit 575f040
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php
Expand Up @@ -13,9 +13,11 @@

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
{
Expand Down Expand Up @@ -80,4 +82,18 @@ public function testItResetsTraces()
$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));
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpClient/TraceableHttpClient.php
Expand Up @@ -67,18 +67,18 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
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
Expand Down

0 comments on commit 575f040

Please sign in to comment.