Skip to content

[HttpClient] Fix handling thrown \Exception in \Generator in MockResponse #44438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
27 changes: 17 additions & 10 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MockResponse implements ResponseInterface
/**
* @param string|string[]|iterable $body The response body as a string or an iterable of strings,
* yielding an empty string simulates an idle timeout,
* exceptions are turned to TransportException
* throwing an exception yields an ErrorChunk
*
* @see ResponseInterface::getInfo() for possible info, e.g. "response_headers"
*/
Expand Down Expand Up @@ -183,6 +183,9 @@ protected static function perform(ClientState $multi, array &$responses): void
$multi->handlesActivity[$id][] = null;
$multi->handlesActivity[$id][] = $e;
}
} elseif ($chunk instanceof \Throwable) {
$multi->handlesActivity[$id][] = null;
$multi->handlesActivity[$id][] = $chunk;
} else {
// Data or timeout chunk
$multi->handlesActivity[$id][] = $chunk;
Expand Down Expand Up @@ -275,16 +278,20 @@ private static function readResponse(self $response, array $options, ResponseInt
$body = $mock instanceof self ? $mock->body : $mock->getContent(false);

if (!\is_string($body)) {
foreach ($body as $chunk) {
if ('' === $chunk = (string) $chunk) {
// simulate an idle timeout
$response->body[] = new ErrorChunk($offset, sprintf('Idle timeout reached for "%s".', $response->info['url']));
} else {
$response->body[] = $chunk;
$offset += \strlen($chunk);
// "notify" download progress
$onProgress($offset, $dlSize, $response->info);
try {
foreach ($body as $chunk) {
if ('' === $chunk = (string) $chunk) {
// simulate an idle timeout
$response->body[] = new ErrorChunk($offset, sprintf('Idle timeout reached for "%s".', $response->info['url']));
} else {
$response->body[] = $chunk;
$offset += \strlen($chunk);
// "notify" download progress
$onProgress($offset, $dlSize, $response->info);
}
}
} catch (\Throwable $e) {
$response->body[] = $e;
}
} elseif ('' !== $body) {
$response->body[] = $body;
Expand Down
45 changes: 44 additions & 1 deletion src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\HttpClient\Tests;

use Symfony\Component\HttpClient\Chunk\DataChunk;
use Symfony\Component\HttpClient\Chunk\ErrorChunk;
use Symfony\Component\HttpClient\Chunk\FirstChunk;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
Expand Down Expand Up @@ -63,6 +66,46 @@ public function invalidResponseFactoryProvider()
];
}

public function testThrowExceptionInBodyGenerator()
{
$mockHttpClient = new MockHttpClient([
new MockResponse((static function (): \Generator {
yield 'foo';
throw new TransportException('foo ccc');
})()),
new MockResponse((static function (): \Generator {
yield 'bar';
throw new \RuntimeException('bar ccc');
})()),
]);

try {
$mockHttpClient->request('GET', 'https://symfony.com', [])->getContent();
$this->fail();
} catch (TransportException $e) {
$this->assertEquals(new TransportException('foo ccc'), $e->getPrevious());
$this->assertSame('foo ccc', $e->getMessage());
}

$chunks = [];
try {
foreach ($mockHttpClient->stream($mockHttpClient->request('GET', 'https://symfony.com', [])) as $chunk) {
$chunks[] = $chunk;
}
$this->fail();
} catch (TransportException $e) {
$this->assertEquals(new \RuntimeException('bar ccc'), $e->getPrevious());
$this->assertSame('bar ccc', $e->getMessage());
}

$this->assertCount(3, $chunks);
$this->assertEquals(new FirstChunk(0, ''), $chunks[0]);
$this->assertEquals(new DataChunk(0, 'bar'), $chunks[1]);
$this->assertInstanceOf(ErrorChunk::class, $chunks[2]);
$this->assertSame(3, $chunks[2]->getOffset());
$this->assertSame('bar ccc', $chunks[2]->getError());
}

protected function getHttpClient(string $testCase): HttpClientInterface
{
$responses = [];
Expand Down Expand Up @@ -167,7 +210,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
case 'testResolve':
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$responses[] = new MockResponse((function () { throw new \Exception('Fake connection timeout'); yield ''; })(), ['response_headers' => $headers]);
$responses[] = new MockResponse((function () { yield ''; })(), ['response_headers' => $headers]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to throw for the test to be effective. Yielding after throwing "breaks" PHP 7.2.

break;

case 'testTimeoutOnStream':
Expand Down