|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\HttpClient\Tests;
|
13 | 13 |
|
| 14 | +use Symfony\Component\HttpClient\Chunk\ErrorChunk; |
14 | 15 | use Symfony\Component\HttpClient\Exception\TransportException;
|
15 | 16 | use Symfony\Component\HttpClient\MockHttpClient;
|
16 | 17 | use Symfony\Component\HttpClient\NativeHttpClient;
|
@@ -63,6 +64,44 @@ public function invalidResponseFactoryProvider()
|
63 | 64 | ];
|
64 | 65 | }
|
65 | 66 |
|
| 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 | + |
66 | 105 | protected function getHttpClient(string $testCase): HttpClientInterface
|
67 | 106 | {
|
68 | 107 | $responses = [];
|
|
0 commit comments