Skip to content

Commit b658da7

Browse files
l-vofabpot
authored andcommitted
[HttpClient] Fix TraceableHttpClient::stream that not works
1 parent a500eb1 commit b658da7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Tests/TraceableHttpClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\HttpClient\NativeHttpClient;
1617
use Symfony\Component\HttpClient\Response\MockResponse;
1718
use Symfony\Component\HttpClient\TraceableHttpClient;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
1921

2022
class TraceableHttpClientTest extends TestCase
2123
{
@@ -80,4 +82,18 @@ public function testItResetsTraces()
8082
$sut->reset();
8183
$this->assertCount(0, $sut->getTracedRequests());
8284
}
85+
86+
public function testStream()
87+
{
88+
TestHttpServer::start();
89+
90+
$sut = new TraceableHttpClient(new NativeHttpClient());
91+
$chunked = $sut->request('GET', 'http://localhost:8057/chunked');
92+
$chunks = [];
93+
foreach ($sut->stream($chunked) as $response) {
94+
$chunks[] = $response->getContent();
95+
}
96+
$this->assertGreaterThan(1, \count($chunks));
97+
$this->assertSame('Symfony is awesome!', implode('', $chunks));
98+
}
8399
}

TraceableHttpClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
6767
if ($responses instanceof TraceableResponse) {
6868
$responses = [$responses];
6969
} elseif (!is_iterable($responses)) {
70-
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
70+
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
7171
}
7272

7373
return $this->client->stream(\Closure::bind(static function () use ($responses) {
7474
foreach ($responses as $k => $r) {
7575
if (!$r instanceof TraceableResponse) {
76-
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
76+
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
7777
}
7878

7979
yield $k => $r->response;
8080
}
81-
}, null, TraceableResponse::class), $timeout);
81+
}, null, TraceableResponse::class)(), $timeout);
8282
}
8383

8484
public function getTracedRequests(): array

0 commit comments

Comments
 (0)