Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpClient/Response/AsyncResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
private $response;
private $info = ['canceled' => false];
private $passthru;
private $lastYielded = false;

/**
* @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
Expand Down Expand Up @@ -255,7 +256,10 @@ public static function stream(iterable $responses, float $timeout = null, string
}
}

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

Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,27 @@ public function testRetryTimeout()

$this->assertSame(200, $response->getStatusCode());
}

public function testRecurciveStream()
{
$client = new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface {
use AsyncDecoratorTrait;

public function request(string $method, string $url, array $options = []): ResponseInterface
{
return new AsyncResponse($this->client, $method, $url, $options);
}
};

$response = $client->request('GET', 'http://localhost:8057/json');
$content = '';
foreach ($client->stream($response) as $chunk) {
$content .= $chunk->getContent();
foreach ($client->stream($response) as $chunk) {
$content .= $chunk->getContent();
}
}

$this->assertSame('{"documents":[{"id":"\/json\/1"},{"id":"\/json\/2"},{"id":"\/json\/3"}]}', $content);
}
}