Skip to content

Commit 0f623af

Browse files
committed
Fix nesteed stream in AsyncResponse
1 parent 0000dfe commit 0f623af

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Component/HttpClient/Response/AsyncResponse.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
3434
private $response;
3535
private $info = ['canceled' => false];
3636
private $passthru;
37+
private $lastYielded = false;
3738

3839
/**
3940
* @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
@@ -237,7 +238,10 @@ public static function stream(iterable $responses, float $timeout = null, string
237238
}
238239
}
239240

240-
if (null === $chunk->getError() && !$chunk->isLast() && $r->response === $response && null !== $r->client) {
241+
if (null === $chunk->getError() && $chunk->isLast()) {
242+
$r->lastYielded = true;
243+
}
244+
if (null === $chunk->getError() && !$r->lastYielded && $r->response === $response && null !== $r->client) {
241245
throw new \LogicException('A chunk passthru must yield an "isLast()" chunk before ending a stream.');
242246
}
243247

src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,27 @@ public function testBufferPurePassthru()
212212
$this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent());
213213
$this->assertStringContainsString('HTTP_HOST', $response->getContent());
214214
}
215+
216+
public function testRecurciveStream()
217+
{
218+
$client = new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface {
219+
use AsyncDecoratorTrait;
220+
221+
public function request(string $method, string $url, array $options = []): ResponseInterface
222+
{
223+
return new AsyncResponse($this->client, $method, $url, $options);
224+
}
225+
};
226+
227+
$response = $client->request('GET', 'http://localhost:8057/json');
228+
$content = '';
229+
foreach($client->stream($response) as $chunk) {
230+
$content.=$chunk->getContent();
231+
foreach($client->stream($response) as $chunk) {
232+
$content.=$chunk->getContent();
233+
}
234+
}
235+
236+
$this->assertSame('{"documents":[{"id":"\/json\/1"},{"id":"\/json\/2"},{"id":"\/json\/3"}]}', $content);
237+
}
215238
}

0 commit comments

Comments
 (0)