From 66dd93cee4ef4a7c76f32207372ea0132695fbe5 Mon Sep 17 00:00:00 2001 From: Gabriel Birke Date: Sat, 21 Mar 2020 14:08:48 +0100 Subject: [PATCH] Expand MockResponse example with HTTP status codes Show how to create MockResponses with status codes different than "200 OK" --- http_client.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/http_client.rst b/http_client.rst index a2056c90976..1738e0d17ba 100644 --- a/http_client.rst +++ b/http_client.rst @@ -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)``).