Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daba023

Browse files
bug #44361 [HttpClient] Fix handling error info in MockResponse (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Fix handling error info in MockResponse | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Using `new MockResponse('', ['error' => 'foobar'])` with the `MockHttpClient`, I expect a `TransportException` to be thrown if I call `getStatusCode()`, `getHeaders()`, `getContent()` or `toArray()`. Currently, it does not work for `getStatusCode()` and `getHeaders()` because `MockResponse` only "converts" the passed error to an exception after the full response workflow has been simulated. `getStatusCode()` and `getHeaders()` stops at the first chunk so it cannot work. I propose to throw at the beginning of the simulated response reading. Commits ------- 369d9d7 [HttpClient] Fix handling error info in MockResponse
2 parents 9de5f79 + 369d9d7 commit daba023

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ private static function readResponse(self $response, array $options, ResponseInt
260260
'http_code' => $response->info['http_code'],
261261
] + $info + $response->info;
262262

263+
if (null !== $response->info['error']) {
264+
throw new TransportException($response->info['error']);
265+
}
266+
263267
if (!isset($response->info['total_time'])) {
264268
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
265269
}

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpClient\Response\ResponseStream;
1919
use Symfony\Contracts\HttpClient\ChunkInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
21-
use Symfony\Contracts\HttpClient\ResponseInterface;
2221

2322
class MockHttpClientTest extends HttpClientTestCase
2423
{
@@ -141,16 +140,8 @@ protected function getHttpClient(string $testCase): HttpClientInterface
141140
break;
142141

143142
case 'testDnsError':
144-
$mock = $this->createMock(ResponseInterface::class);
145-
$mock->expects($this->any())
146-
->method('getStatusCode')
147-
->willThrowException(new TransportException('DSN error'));
148-
$mock->expects($this->any())
149-
->method('getInfo')
150-
->willReturn([]);
151-
152-
$responses[] = $mock;
153-
$responses[] = $mock;
143+
$responses[] = $mockResponse = new MockResponse('', ['error' => 'DNS error']);
144+
$responses[] = $mockResponse;
154145
break;
155146

156147
case 'testToStream':
@@ -164,12 +155,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
164155
break;
165156

166157
case 'testTimeoutOnAccess':
167-
$mock = $this->createMock(ResponseInterface::class);
168-
$mock->expects($this->any())
169-
->method('getHeaders')
170-
->willThrowException(new TransportException('Timeout'));
171-
172-
$responses[] = $mock;
158+
$responses[] = new MockResponse('', ['error' => 'Timeout']);
173159
break;
174160

175161
case 'testAcceptHeader':
@@ -231,16 +217,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
231217
break;
232218

233219
case 'testMaxDuration':
234-
$mock = $this->createMock(ResponseInterface::class);
235-
$mock->expects($this->any())
236-
->method('getContent')
237-
->willReturnCallback(static function (): void {
238-
usleep(100000);
239-
240-
throw new TransportException('Max duration was reached.');
241-
});
242-
243-
$responses[] = $mock;
220+
$responses[] = new MockResponse('', ['error' => 'Max duration was reached.']);
244221
break;
245222
}
246223

src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\HttpClient\Exception\JsonException;
7+
use Symfony\Component\HttpClient\Exception\TransportException;
78
use Symfony\Component\HttpClient\Response\MockResponse;
89

910
/**
@@ -83,4 +84,14 @@ public function toArrayErrors()
8384
'message' => 'JSON content was expected to decode to an array, "integer" returned for "https://example.com/file.json".',
8485
];
8586
}
87+
88+
public function testErrorIsTakenIntoAccountInInitialization()
89+
{
90+
$this->expectException(TransportException::class);
91+
$this->expectExceptionMessage('ccc error');
92+
93+
MockResponse::fromRequest('GET', 'https://symfony.com', [], new MockResponse('', [
94+
'error' => 'ccc error',
95+
]))->getStatusCode();
96+
}
8697
}

0 commit comments

Comments
 (0)