From 3c93764f109b800fdc64a99bfa0f7ec6ed7f4e04 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 19 Sep 2019 20:45:47 +0200 Subject: [PATCH] [HttpClient] fix throwing HTTP exceptions when the 1st chunk is emitted --- .../HttpClient/Response/ResponseTrait.php | 22 +++++++++++++------ .../HttpClient/Test/HttpClientTestCase.php | 10 +++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php index bf7ba3a746..8a1d9a9077 100644 --- a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php +++ b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php @@ -317,9 +317,20 @@ trait ResponseTrait } elseif ($chunk instanceof ErrorChunk) { unset($responses[$j]); $isTimeout = true; - } elseif ($chunk instanceof FirstChunk && $response->logger) { - $info = $response->getInfo(); - $response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url'])); + } elseif ($chunk instanceof FirstChunk) { + if ($response->logger) { + $info = $response->getInfo(); + $response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url'])); + } + + yield $response => $chunk; + + if ($response->initializer && null === $response->info['error']) { + // Ensure the HTTP status code is always checked + $response->getHeaders(true); + } + + continue; } yield $response => $chunk; @@ -327,10 +338,7 @@ trait ResponseTrait unset($multi->handlesActivity[$j]); - if ($chunk instanceof FirstChunk && null === $response->initializer && null === $response->info['error']) { - // Ensure the HTTP status code is always checked - $response->getHeaders(true); - } elseif ($chunk instanceof ErrorChunk && !$chunk->didThrow()) { + if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) { // Ensure transport exceptions are always thrown $chunk->getContent(); } diff --git a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php index 932614dff3..0547c752ac 100644 --- a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php +++ b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php @@ -152,6 +152,16 @@ abstract class HttpClientTestCase extends TestCase $this->assertSame(404, $response->getStatusCode()); $this->assertSame(['application/json'], $response->getHeaders(false)['content-type']); $this->assertNotEmpty($response->getContent(false)); + + $response = $client->request('GET', 'http://localhost:8057/404'); + + try { + foreach ($client->stream($response) as $chunk) { + $this->assertTrue($chunk->isFirst()); + } + $this->fail(ClientExceptionInterface::class.' expected'); + } catch (ClientExceptionInterface $e) { + } } public function testIgnoreErrors()