Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bbfe9d8

Browse filesBrowse files
committed
[HttpClient] Allow yielding Exception from MockResponse's $body to mock transport errors
1 parent 3d8efd5 commit bbfe9d8
Copy full SHA for bbfe9d8

File tree

3 files changed

+49
-3
lines changed
Filter options

3 files changed

+49
-3
lines changed

‎src/Symfony/Component/HttpClient/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/CHANGELOG.md
+5Lines 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 yielding `Exception` from MockResponse's `$body` to mock transport errors
8+
49
5.4
510
---
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/MockResponse.php
+7-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
+37Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.