|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\HttpClient\Tests;
|
13 | 13 |
|
| 14 | +use Symfony\Component\HttpClient\Chunk\DataChunk; |
| 15 | +use Symfony\Component\HttpClient\Chunk\ErrorChunk; |
| 16 | +use Symfony\Component\HttpClient\Chunk\FirstChunk; |
14 | 17 | use Symfony\Component\HttpClient\Exception\TransportException;
|
15 | 18 | use Symfony\Component\HttpClient\MockHttpClient;
|
16 | 19 | use Symfony\Component\HttpClient\NativeHttpClient;
|
@@ -184,6 +187,46 @@ public function invalidResponseFactoryProvider()
|
184 | 187 | ];
|
185 | 188 | }
|
186 | 189 |
|
| 190 | + public function testThrowExceptionInBodyGenerator() |
| 191 | + { |
| 192 | + $mockHttpClient = new MockHttpClient([ |
| 193 | + new MockResponse((static function (): \Generator { |
| 194 | + yield 'foo'; |
| 195 | + throw new TransportException('foo ccc'); |
| 196 | + })()), |
| 197 | + new MockResponse((static function (): \Generator { |
| 198 | + yield 'bar'; |
| 199 | + throw new \RuntimeException('bar ccc'); |
| 200 | + })()), |
| 201 | + ]); |
| 202 | + |
| 203 | + try { |
| 204 | + $mockHttpClient->request('GET', 'https://symfony.com', [])->getContent(); |
| 205 | + $this->fail(); |
| 206 | + } catch (TransportException $e) { |
| 207 | + $this->assertEquals(new TransportException('foo ccc'), $e->getPrevious()); |
| 208 | + $this->assertSame('foo ccc', $e->getMessage()); |
| 209 | + } |
| 210 | + |
| 211 | + $chunks = []; |
| 212 | + try { |
| 213 | + foreach ($mockHttpClient->stream($mockHttpClient->request('GET', 'https://symfony.com', [])) as $chunk) { |
| 214 | + $chunks[] = $chunk; |
| 215 | + } |
| 216 | + $this->fail(); |
| 217 | + } catch (TransportException $e) { |
| 218 | + $this->assertEquals(new \RuntimeException('bar ccc'), $e->getPrevious()); |
| 219 | + $this->assertSame('bar ccc', $e->getMessage()); |
| 220 | + } |
| 221 | + |
| 222 | + $this->assertCount(3, $chunks); |
| 223 | + $this->assertEquals(new FirstChunk(0, ''), $chunks[0]); |
| 224 | + $this->assertEquals(new DataChunk(0, 'bar'), $chunks[1]); |
| 225 | + $this->assertInstanceOf(ErrorChunk::class, $chunks[2]); |
| 226 | + $this->assertSame(3, $chunks[2]->getOffset()); |
| 227 | + $this->assertSame('bar ccc', $chunks[2]->getError()); |
| 228 | + } |
| 229 | + |
187 | 230 | protected function getHttpClient(string $testCase): HttpClientInterface
|
188 | 231 | {
|
189 | 232 | $responses = [];
|
@@ -299,7 +342,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
|
299 | 342 | case 'testResolve':
|
300 | 343 | $responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
301 | 344 | $responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
302 |
| - $responses[] = new MockResponse((function () { throw new \Exception('Fake connection timeout'); yield ''; })(), ['response_headers' => $headers]); |
| 345 | + $responses[] = new MockResponse((function () { yield ''; })(), ['response_headers' => $headers]); |
303 | 346 | break;
|
304 | 347 |
|
305 | 348 | case 'testTimeoutOnStream':
|
|
0 commit comments