Skip to content

Commit e7bb5db

Browse files
SirRFIrafal
authored and
rafal
committed
[HttpClient] Add information about request testing
1 parent 869b4a2 commit e7bb5db

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

http_client.rst

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,8 @@ This allows using them where native PHP streams are needed::
13871387
// later on if you need to, you can access the response from the stream
13881388
$response = stream_get_meta_data($streamResource)['wrapper_data']->getResponse();
13891389

1390-
Testing HTTP Clients and Responses
1391-
----------------------------------
1390+
Testing
1391+
-------
13921392

13931393
This component includes the ``MockHttpClient`` and ``MockResponse`` classes to
13941394
use them in tests that shouldn't make actual HTTP requests. Such tests can be
@@ -1402,7 +1402,10 @@ HTTP client in this component. You can use the interface to accept real client
14021402
in your code, while replacing it with ``MockHttpClient`` in the test.
14031403

14041404
When the ``request`` method is used on ``MockHttpClient``, it will respond with
1405-
supplied ``MockResponse``. There are few ways to use it:
1405+
supplied ``MockResponse``. There are few ways to use it, as described below.
1406+
1407+
HTTP Client and Responses
1408+
~~~~~~~~~~~~~~~~~~~~~~~~~
14061409

14071410
The first way of using ``MockHttpClient`` is to pass a list of responses to its
14081411
constructor. These will be yielded in order when requests are made::
@@ -1461,6 +1464,39 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
14611464

14621465
$mockResponse = new MockResponse($body());
14631466

1467+
Testing Request Data
1468+
~~~~~~~~~~~~~~~~~~~~
1469+
1470+
The examples above describe how to return desired response. What if you wanted
1471+
to also test the request itself? ``MockResponse`` comes with helper methods:
1472+
1473+
* ``getRequestMethod()`` - returns HTTP method
1474+
* ``getRequestUrl()`` - returns URL the request would be sent to
1475+
* ``getRequestOptions()`` - returns an array containing other information about
1476+
the request such as headers, query parameters, body content etc.
1477+
1478+
Usage example::
1479+
1480+
$mockResponse = new MockResponse('', ['http_code' => 204]);
1481+
$httpClient = new MockHttpClient($mockResponse, 'https://example.com/');
1482+
1483+
$response = $httpClient->request('DELETE', '/api/article/1337', [
1484+
'headers' => [
1485+
'Accept: */*',
1486+
'Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l',
1487+
],
1488+
]);
1489+
1490+
// returns "DELETE"
1491+
$mockResponse->getRequestMethod();
1492+
1493+
// returns "https://example.com//api/article/1337"
1494+
$mockResponse->getRequestUrl();
1495+
1496+
// returns ["Accept: */*", "Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l"]
1497+
$mockResponse->getRequestOptions()['headers'];
1498+
1499+
14641500
.. _`cURL PHP extension`: https://www.php.net/curl
14651501
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/
14661502
.. _`PSR-18`: https://www.php-fig.org/psr/psr-18/

0 commit comments

Comments
 (0)