8000 [HttpClient] throw exception when AsyncDecoratorTrait gets an already… · symfony/symfony@81bfa6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 81bfa6f

Browse files
[HttpClient] throw exception when AsyncDecoratorTrait gets an already consumed response
1 parent 05a7020 commit 81bfa6f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
3636
private $info = ['canceled' => false];
3737
private $passthru;
3838
private $stream;
39-
private $lastYielded = false;
39+
private $yieldedState;
4040

4141
/**
4242
* @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
@@ -272,6 +272,14 @@ public static function stream(iterable $responses, float $timeout = null, string
272272
continue;
273273
}
274274

275+
i 8000 f (null !== $chunk->getError()) {
276+
// no-op
277+
} elseif ($chunk->isFirst()) {
278+
$r->yieldedState = 1;
279+
} elseif (1 !== $r->yieldedState && null === $chunk->getInformationalStatus()) {
280+
throw new \LogicException(sprintf('Instance of "%s" is already consumed and cannot be managed by "%s". A decorated client should not call any of the response\'s methods in its "request()" method.', get_debug_type($response), $class ?? static::class));
281+
}
282+
275283
foreach (self::passthru($r->client, $r, $chunk, $asyncMap) as $chunk) {
276284
yield $r => $chunk;
277285
}
@@ -282,9 +290,9 @@ public static function stream(iterable $responses, float $timeout = null, string
282290
}
283291

284292
if (null === $chunk->getError() && $chunk->isLast()) {
285-
$r->lastYielded = true;
293+
$r->yieldedState = 2;
286294
}
287-
if (null === $chunk->getError() && !$r->lastYielded && $r->response === $response && null !== $r->client) {
295+
if (null === $chunk->getError() && 2 !== $r->yieldedState && $r->response === $response && null !== $r->client) {
288296
throw new \LogicException('A chunk passthru must yield an "isLast()" chunk before ending a stream.');
289297
}
290298

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
2121
use Symfony\Contracts\HttpClient\ResponseInterface;
22+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
2223

2324
class AsyncDecoratorTraitTest extends NativeHttpClientTest
2425
{
25-
protected function getHttpClient(string $testCase, \Closure $chunkFilter = null): HttpClientInterface
26+
protected function getHttpClient(string $testCase, \Closure $chunkFilter = null, HttpClientInterface $decoratedClient = null): HttpClientInterface
2627
{
2728
if ('testHandleIsRemovedOnException' === $testCase) {
2829
$this->markTestSkipped("AsyncDecoratorTrait doesn't cache handles");
2930
}
3031

3132
$chunkFilter = $chunkFilter ?? static function (ChunkInterface $chunk, AsyncContext $context) { yield $chunk; };
3233

33-
return new class(parent::getHttpClient($testCase), $chunkFilter) implements HttpClientInterface {
34+
return new class($decoratedClient ?? parent::getHttpClient($testCase), $chunkFilter) implements HttpClientInterface {
3435
use AsyncDecoratorTrait;
3536

3637
private $chunkFilter;
@@ -303,4 +304,30 @@ public function testMultipleYieldInInitializer()
303304
$this->assertSame(404, $response->getStatusCode());
304305
$this->assertStringContainsString('injectedFoo', $response->getContent(false));
305306
}
307+
308+
pub 8000 lic function testConsumingDecoratedClient()
309+
{
310+
$client = $this->getHttpClient(__FUNCTION__, null, new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface {
311+
use AsyncDecoratorTrait;
312+
313+
public function request(string $method, string $url, array $options = []): ResponseInterface
314+
{
315+
$response = $this->client->request($method, $url, $options);
316+
$response->getStatusCode(); // should be avoided and breaks compatibility with AsyncDecoratorTrait
317+
318+
return $response;
319+
}
320+
321+
public function stream($responses, float $timeout = null): ResponseStreamInterface
322+
{
323+
return $this->client->stream($responses, $timeout);
324+
}
325+
});
326+
327+
$response = $client->request('GET', 'http://localhost:8057/');
328+
329+
$this->expectException(\LogicException::class);
330+
$this->expectExceptionMessage('Instance of "Symfony\Component\HttpClient\Response\NativeResponse" is already consumed and cannot be managed by "Symfony\Component\HttpClient\Response\AsyncResponse". A decorated client should not call any of the response\'s methods in its "request()" method.');
331+
$response->getStatusCode();
332+
}
306333
}

0 commit comments

Comments
 (0)
0