[HttpClient] fix throwing HTTP exceptions when the 1st chunk is emitted

This commit is contained in:
Nicolas Grekas 2019-09-19 20:45:47 +02:00
parent 9d882e8ce2
commit 3c93764f10
2 changed files with 25 additions and 7 deletions

View File

@ -317,9 +317,20 @@ trait ResponseTrait
} elseif ($chunk instanceof ErrorChunk) { } elseif ($chunk instanceof ErrorChunk) {
unset($responses[$j]); unset($responses[$j]);
$isTimeout = true; $isTimeout = true;
} elseif ($chunk instanceof FirstChunk && $response->logger) { } elseif ($chunk instanceof FirstChunk) {
$info = $response->getInfo(); if ($response->logger) {
$response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url'])); $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; yield $response => $chunk;
@ -327,10 +338,7 @@ trait ResponseTrait
unset($multi->handlesActivity[$j]); unset($multi->handlesActivity[$j]);
if ($chunk instanceof FirstChunk && null === $response->initializer && null === $response->info['error']) { if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
// Ensure the HTTP status code is always checked
$response->getHeaders(true);
} elseif ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
// Ensure transport exceptions are always thrown // Ensure transport exceptions are always thrown
$chunk->getContent(); $chunk->getContent();
} }

View File

@ -152,6 +152,16 @@ abstract class HttpClientTestCase extends TestCase
$this->assertSame(404, $response->getStatusCode()); $this->assertSame(404, $response->getStatusCode());
$this->assertSame(['application/json'], $response->getHeaders(false)['content-type']); $this->assertSame(['application/json'], $response->getHeaders(false)['content-type']);
$this->assertNotEmpty($response->getContent(false)); $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() public function testIgnoreErrors()