Skip to content

[HttpClient] Expand MockResponse example with HTTP status codes #13387

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
Jan 8, 2021
Merged
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
15 changes: 15 additions & 0 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,21 @@ responses dynamically when it's called::
$client = new MockHttpClient($callback);
$response = $client->request('...'); // calls $callback to get the response

If you need to test your error handling with responses that have an HTTP status
code different than 200, you need to provide a raw HTTP header with the
`response_headers` key::

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

$client = new MockHttpClient([
new MockResponse('...', ['response_headers' => ['HTTP/1.1 500 Internal Server Error']]),
new MockResponse('...', ['response_headers' => ['HTTP/1.1 404 Page Not Found']]),
]);

$response = $client->request('...');


The responses provided to the mock client don't have to be instances of
``MockResponse``. Any class implementing ``ResponseInterface`` will work (e.g.
``$this->createMock(ResponseInterface::class)``).
Expand Down