Skip to content

Commit 3beaf09

Browse files
committed
[HttpClient] Fix handling thrown \Exception in \Generator in MockResponse
1 parent 677ca9c commit 3beaf09

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MockResponse implements ResponseInterface
3838
/**
3939
* @param string|string[]|iterable $body The response body as a string or an iterable of strings,
4040
* yielding an empty string simulates an idle timeout,
41-
* exceptions are turned to TransportException
41+
* exceptions are turned to ErrorChunk
4242
*
4343
* @see ResponseInterface::getInfo() for possible info, e.g. "response_headers"
4444
*/

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient\Tests;
1313

14+
use Symfony\Component\HttpClient\Chunk\ErrorChunk;
1415
use Symfony\Component\HttpClient\Exception\TransportException;
1516
use Symfony\Component\HttpClient\MockHttpClient;
1617
use Symfony\Component\HttpClient\NativeHttpClient;
@@ -63,6 +64,44 @@ public function invalidResponseFactoryProvider()
6364
];
6465
}
6566

67+
public function testThrowExceptionInBodyGenerator()
68+
{
69+
$mockHttpClient = new MockHttpClient([
70+
new MockResponse((static function (): \Generator {
71+
yield 'foo';
72+
throw new TransportException('foo ccc');
73+
})()),
74+
new MockResponse((static function (): \Generator {
75+
yield 'bar';
76+
throw new \RuntimeException('bar ccc');
77+
})()),
78+
]);
79+
80+
try {
81+
$mockHttpClient->request('GET', 'https://symfony.com', [])->getContent();
82+
$this->fail();
83+
} catch (TransportException $e) {
84+
$this->assertEquals(new TransportException('foo ccc'), $e->getPrevious());
85+
$this->assertSame('foo ccc', $e->getMessage());
86+
}
87+
88+
$chunks = [];
89+
try {
90+
foreach ($mockHttpClient->stream($mockHttpClient->request('GET', 'https://symfony.com', [])) as $chunk) {
91+
$chunks[] = $chunk;
92+
}
93+
$this->fail();
94+
} catch (TransportException $e) {
95+
$this->assertEquals(new \RuntimeException('bar ccc'), $e->getPrevious());
96+
$this->assertSame('bar ccc', $e->getMessage());
97+
}
98+
99+
$this->assertCount(1, $chunks);
100+
$this->assertInstanceOf(ErrorChunk::class, $chunks[0]);
101+
$this->assertSame(0, $chunks[0]->getOffset());
102+
$this->assertSame('bar ccc', $chunks[0]->getError());
103+
}
104+
66105
protected function getHttpClient(string $testCase): HttpClientInterface
67106
{
68107
$responses = [];

0 commit comments

Comments
 (0)