Skip to content

Commit 91d63c3

Browse files
authored
Expand MockResponse example with HTTP status codes
Show how to create MockResponses with status codes different than "200 OK"
1 parent b237771 commit 91d63c3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

components/http_client.rst

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

1004+
If you need to test your error handling with responses that have an HTTP status
1005+
code different than 200, you need to provide a raw HTTP header with the
1006+
`response_headers` key::
1007+
1008+
use Symfony\Component\HttpClient\MockHttpClient;
1009+
use Symfony\Component\HttpClient\Response\MockResponse;
1010+
1011+
$client = new MockHttpClient([
1012+
new MockResponse('...', ['response_headers' => ['HTTP/1.1 500 Internal Server Error']]),
1013+
new MockResponse('...', ['response_headers' => ['HTTP/1.1 404 Page Not Found']]),
1014+
]);
1015+
1016+
$response = $client->request('...');
1017+
1018+
10041019
The responses provided to the mock client don't have to be instances of
10051020
``MockResponse``. Any class implementing ``ResponseInterface`` will work (e.g.
10061021
``$this->createMock(ResponseInterface::class)``).

0 commit comments

Comments
 (0)