|
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;
|
@@ -63,6 +66,46 @@ public function invalidResponseFactoryProvider()
|
63 | 66 | ];
|
64 | 67 | }
|
65 | 68 |
|
| 69 | + public function testThrowExceptionInBodyGenerator() |
| 70 | + { |
| 71 | + $mockHttpClient = new MockHttpClient([ |
| 72 | + new MockResponse((static function (): \Generator { |
| 73 | + yield 'foo'; |
| 74 | + throw new TransportException('foo ccc'); |
| 75 | + })()), |
| 76 | + new MockResponse((static function (): \Generator { |
| 77 | + yield 'bar'; |
| 78 | + throw new \RuntimeException('bar ccc'); |
| 79 | + })()), |
| 80 | + ]); |
| 81 | + |
| 82 | + try { |
| 83 | + $mockHttpClient->request('GET', 'https://symfony.com', [])->getContent(); |
| 84 | + $this->fail(); |
| 85 | + } catch (TransportException $e) { |
| 86 | + $this->assertEquals(new TransportException('foo ccc'), $e->getPrevious()); |
| 87 | + $this->assertSame('foo ccc', $e->getMessage()); |
| 88 | + } |
| 89 | + |
| 90 | + $chunks = []; |
| 91 | + try { |
| 92 | + foreach ($mockHttpClient->stream($mockHttpClient->request('GET', 'https://symfony.com', [])) as $chunk) { |
| 93 | + $chunks[] = $chunk; |
| 94 | + } |
| 95 | + $this->fail(); |
| 96 | + } catch (TransportException $e) { |
| 97 | + $this->assertEquals(new \RuntimeException('bar ccc'), $e->getPrevious()); |
| 98 | + $this->assertSame('bar ccc', $e->getMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + $this->assertCount(3, $chunks); |
| 102 | + $this->assertEquals(new FirstChunk(0, ''), $chunks[0]); |
| 103 | + $this->assertEquals(new DataChunk(0, 'bar'), $chunks[1]); |
| 104 | + $this->assertInstanceOf(ErrorChunk::class, $chunks[2]); |
| 105 | + $this->assertSame(3, $chunks[2]->getOffset()); |
| 106 | + $this->assertSame('bar ccc', $chunks[2]->getError()); |
| 107 | + } |
| 108 | + |
66 | 109 | protected function getHttpClient(string $testCase): HttpClientInterface
|
67 | 110 | {
|
68 | 111 | $responses = [];
|
@@ -167,7 +210,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
|
167 | 210 | case 'testResolve':
|
168 | 211 | $responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
169 | 212 | $responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
170 |
| - $responses[] = new MockResponse((function () { throw new \Exception('Fake connection timeout'); yield ''; })(), ['response_headers' => $headers]); |
| 213 | + $responses[] = new MockResponse((function () { yield ''; })(), ['response_headers' => $headers]); |
171 | 214 | break;
|
172 | 215 |
|
173 | 216 | case 'testTimeoutOnStream':
|
|
0 commit comments