Skip to content

Commit 638a96e

Browse files
committed
minor #18607 [HttpClient] Backport HAR files feature (GaryPEGEOT, HeahDude)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpClient] Backport HAR files feature #18547 should have been merged in 6.4, I've cherry-picked the commit to backport it, while fixing the version added directive. Commits ------- 8385108 [HttpClient] fix version added for HAR files feature 948966d [HTTPClient] Add documentation for `HarFileResponseFactory`
2 parents 60bb21d + 8385108 commit 638a96e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

http_client.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,45 @@ test it in a real application::
21962196
}
21972197
}
21982198

2199+
Testing using HAR files
2200+
~~~~~~~~~~~~~~~~~~~~~~~
2201+
2202+
The previous example can also be achieved using `HAR`_ files. You can export those files from all modern browsers (from the network tab) &
2203+
HTTP Clients. First, do the HTTP request(s) you want to test in your favorite HTTP Client / Browser, then store generated ``.har`` file somewhere in your application::
2204+
2205+
// ExternalArticleServiceTest.php
2206+
use PHPUnit\Framework\TestCase;
2207+
use Symfony\Component\HttpClient\MockHttpClient;
2208+
use Symfony\Component\HttpClient\Response\MockResponse;
2209+
2210+
final class ExternalArticleServiceTest extends TestCase
2211+
{
2212+
public function testSubmitData(): void
2213+
{
2214+
// Arrange
2215+
$fixtureDir = sprintf('%s/tests/fixtures/HTTP', static::getContainer()->getParameter('kernel.project_dir'));
2216+
$factory = new HarFileResponseFactory("$fixtureDir/example.com_archive.har");
2217+
$httpClient = new MockHttpClient($factory, 'https://example.com');
2218+
$service = new ExternalArticleService($httpClient);
2219+
2220+
// Act
2221+
$responseData = $service->createArticle($requestData);
2222+
2223+
// Assert
2224+
self::assertSame($responseData, 'the expected response');
2225+
}
2226+
}
2227+
2228+
2229+
If your service does multiple requests or if your `.har` file contains multiple request / response pairs,
2230+
the :class:`Symfony\\Component\\HttpClient\\Test\\HarFileResponseFactory` will find the associated response based on
2231+
the request method, url and body (if any). Note that **this doesn't work** if the request body or uri is random / always changing
2232+
(if it contains current date or random UUID(s) for example).
2233+
2234+
.. versionadded:: 6.4
2235+
2236+
The ``HarFileResponseFactory`` was introduced in Symfony 6.4.
2237+
21992238
Testing Network Transport Exceptions
22002239
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22012240

@@ -2262,3 +2301,4 @@ you to do so, by yielding the exception from its body::
22622301
.. _`idempotent method`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods
22632302
.. _`SSRF`: https://portswigger.net/web-security/ssrf
22642303
.. _`RFC 6570`: https://www.rfc-editor.org/rfc/rfc6570
2304+
.. _`HAR`: https://w3c.github.io/web-performance/specs/HAR/Overview.html

0 commit comments

Comments
 (0)