Skip to content

[HttpClient] Fix handling error info in MockResponse #44361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ private static function readResponse(self $response, array $options, ResponseInt
'http_code' => $response->info['http_code'],
] + $info + $response->info;

if (null !== $response->info['error']) {
throw new TransportException($response->info['error']);
}

if (!isset($response->info['total_time'])) {
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
}
Expand Down
31 changes: 4 additions & 27 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\ChunkInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class MockHttpClientTest extends HttpClientTestCase
{
Expand Down Expand Up @@ -141,16 +140,8 @@ protected function getHttpClient(string $testCase): HttpClientInterface
break;

case 'testDnsError':
$mock = $this->createMock(ResponseInterface::class);
$mock->expects($this->any())
->method('getStatusCode')
->willThrowException(new TransportException('DSN error'));
$mock->expects($this->any())
->method('getInfo')
->willReturn([]);

$responses[] = $mock;
$responses[] = $mock;
$responses[] = $mockResponse = new MockResponse('', ['error' => 'DNS error']);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to mock anymore because getStatusCode() will throw properly.

$responses[] = $mockResponse;
break;

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

case 'testTimeoutOnAccess':
$mock = $this->createMock(ResponseInterface::class);
$mock->expects($this->any())
->method('getHeaders')
->willThrowException(new TransportException('Timeout'));

$responses[] = $mock;
$responses[] = new MockResponse('', ['error' => 'Timeout']);
break;

case 'testAcceptHeader':
Expand Down Expand Up @@ -231,16 +217,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
break;

case 'testMaxDuration':
$mock = $this->createMock(ResponseInterface::class);
$mock->expects($this->any())
->method('getContent')
->willReturnCallback(static function (): void {
usleep(100000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This usleep was useless.


throw new TransportException('Max duration was reached.');
});

$responses[] = $mock;
$responses[] = new MockResponse('', ['error' => 'Max duration was reached.']);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Response\MockResponse;

/**
Expand Down Expand Up @@ -83,4 +84,14 @@ public function toArrayErrors()
'message' => 'JSON content was expected to decode to an array, "integer" returned for "https://example.com/file.json".',
];
}

public function testErrorIsTakenIntoAccountInInitialization()
{
$this->expectException(TransportException::class);
$this->expectExceptionMessage('ccc error');

MockResponse::fromRequest('GET', 'https://symfony.com', [], new MockResponse('', [
'error' => 'ccc error',
]))->getStatusCode();
}
}