Skip to content

Commit c3250a1

Browse files
committed
minor #13387 [HttpClient] Expand MockResponse example with HTTP status codes (gbirke)
This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead. Discussion ---------- [HttpClient] Expand MockResponse example with HTTP status codes Commits ------- 66dd93c Expand MockResponse example with HTTP status codes
2 parents 21d3087 + 66dd93c commit c3250a1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

http_client.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,21 @@ responses dynamically when it's called::
13871387
$client = new MockHttpClient($callback);
13881388
$response = $client->request('...'); // calls $callback to get the response
13891389

1390+
If you need to test your error handling with responses that have an HTTP status
1391+
code different than 200, you need to provide a raw HTTP header with the
1392+
`response_headers` key::
1393+
1394+
use Symfony\Component\HttpClient\MockHttpClient;
1395+
use Symfony\Component\HttpClient\Response\MockResponse;
1396+
1397+
$client = new MockHttpClient([
1398+
new MockResponse('...', ['response_headers' => ['HTTP/1.1 500 Internal Server Error']]),
1399+
new MockResponse('...', ['response_headers' => ['HTTP/1.1 404 Page Not Found']]),
1400+
]);
1401+
1402+
$response = $client->request('...');
1403+
1404+
13901405
The responses provided to the mock client don't have to be instances of
13911406
``MockResponse``. Any class implementing ``ResponseInterface`` will work (e.g.
13921407
``$this->createMock(ResponseInterface::class)``).

0 commit comments

Comments
 (0)