8000 [HttpClient] Allow passing an exception directly in the MockResponse … · symfony/symfony@13d234c · GitHub
[go: up one dir, main page]

Skip to content

Commit 13d234c

Browse files
committed
[HttpClient] Allow passing an exception directly in the MockResponse body
1 parent cdeba5a commit 13d234c

File tree

3 files changed

+49
< 8000 div class="ml-1 text-small text-bold fgColor-danger">-3
lines changed

3 files changed

+49
-3
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Allow passing `\Exception` directly in the body of `MockResponse`
8+
49
5.4
510
---
611

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class MockResponse implements ResponseInterface, StreamableInterface
3939
private static int $idSequence = 0;
4040

4141
/**
42-
* @param string|string[]|iterable $body The response body as a string or an iterable of strings,
43-
* yielding an empty string simulates an idle timeout,
44-
* throwing an exception yields an ErrorChunk
42+
* @param string|iterable<string|\Throwable> $body The response body as a string or an iterable of strings,
43+
* yielding an empty string simulates an idle timeout,
44+
* throwing or yielding an exception yields an ErrorChunk
4545
*
4646
* @see ResponseInterface::getInfo() for possible info, e.g. "response_headers"
4747
*/
@@ -305,6 +305,10 @@ private static function readResponse(self $response, array $options, ResponseInt
305305
if (!\is_string($body)) {
306306
try {
307307
foreach ($body as $chunk) {
308+
if ($chunk instanceof \Throwable) {
309+
throw $chunk;
310+
}
311+
308312
if ('' === $chunk = (string) $chunk) {
309313
// simulate an idle timeout
310314
$response->body[] = new ErrorChunk($offset, sprintf('Idle timeout reached for "%s".', $response->info['url']));

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,43 @@ public function testThrowExceptionInBodyGenerator()
227227
$this->assertSame('bar ccc', $chunks[2]->getError());
228228
}
229229

230+
public function testExceptionDirectlyInBody()
231+
{
232+
$mockHttpClient = new MockHttpClient([
233+
new MockResponse(['foo', new \RuntimeException('foo ccc')]),
234+
new MockResponse((static function (): \Generator {
235+
yield 'bar';
236+
yield new TransportException('bar ccc');
237+
})()),
238+
]);
239+
240+
try {
241+
$mockHttpClient->request('GET', 'https://symfony.com', [])->getContent();
242+
$this->fail();
243+
} catch (TransportException $e) {
244+
$this->assertEquals(new \RuntimeException('foo ccc'), $e->getPrevious());
245+
$this->assertSame('foo ccc', $e->getMessage());
246+
}
247+
248+
$chunks = [];
249+
try {
250+
foreach ($mockHttpClient->stream($mockHttpClient->request('GET', 'https://symfony.com', [])) as $chunk) {
251+
$chunks[] = $chunk;
252+
}
253+
$this->fail();
254+
} catch (TransportException $e) {
255+
$this->assertEquals(new TransportException('bar ccc'), $e->getPrevious());
256+
$this->assertSame('bar ccc', $e->getMessage());
257+
}
258+
259+
$this->assertCount(3, $chunks);
260+
$this->assertEquals(new FirstChunk(0, ''), $chunks[0]);
261+
$this->assertEquals(new DataChunk(0, 'bar'), $chunks[1]);
262+
$this->assertInstanceOf(ErrorChunk::class, $chunks[2]);
263+
$this->assertSame(3, $chunks[2]->getOffset());
264+
$this->assertSame('bar ccc', $chunks[2]->getError());
265+
}
266+
230267
protected function getHttpClient(string $testCase): HttpClientInterface
231268
{
232269
$responses = [];

0 commit comments

Comments
 (0)
0