Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c63faff

Browse filesBrowse files
committed
feature #50807 [HttpClient] Add an HAR response factory for testing (GaryPEGEOT)
This PR was squashed before being merged into the 7.0 branch. Discussion ---------- [HttpClient] Add an HAR response factory for testing | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | TODO From the network tab of most modern browsers, you can save one or more request as an HAR archive file. This pull request adds a way to use those files in test to replay responses previously saved. Example usage: ```php // Inside a KernelTestCase // ... $factory = new HarFileResponseFactory('path/to/some_file.har'); static::getContainer()->get('http_client')->setResponseFactory($factory); // Will find the suitable response(s) inside some_file.har $myServiceWithHttpCalls->doSomething(); ``` Commits ------- 01d2e35 [HttpClient] Add an HAR response factory for testing
2 parents d0c73cc + 01d2e35 commit c63faff
Copy full SHA for c63faff

File tree

6 files changed

+1117
-0
lines changed
Filter options

6 files changed

+1117
-0
lines changed

‎src/Symfony/Component/HttpClient/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Remove implementing `Http\Message\RequestFactory` from `HttplugClient`
8+
* Add `HarFileResponseFactory` testing utility, allow to replay responses from `.har` files
89

910
6.4
1011
---
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Test;
13+
14+
use Symfony\Component\HttpClient\Exception\TransportException;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
16+
use Symfony\Contracts\HttpClient\ResponseInterface;
17+
18+
/**
19+
* See: https://w3c.github.io/web-performance/specs/HAR/Overview.html.
20+
*
21+
* @author Gary PEGEOT <garypegeot@gmail.com>
22+
*/
23+
class HarFileResponseFactory
24+
{
25+
public function __construct(private string $archiveFile)
26+
{
27+
}
28+
29+
public function setArchiveFile(string $archiveFile): void
30+
{
31+
$this->archiveFile = $archiveFile;
32+
}
33+
34+
public function __invoke(string $method, string $url, array $options): ResponseInterface
35+
{
36+
if (!is_file($this->archiveFile)) {
37+
throw new \InvalidArgumentException(sprintf('Invalid file path provided: "%s".', $this->archiveFile));
38+
}
39+
40+
$json = json_decode(json: file_get_contents($this->archiveFile), associative: true, flags: \JSON_THROW_ON_ERROR);
41+
42+
foreach ($json['log']['entries'] as $entry) {
43+
/**
44+
* @var array{status: int, headers: array, content: array} $response
45+
* @var array{method: string, url: string, postData: array} $request
46+
*/
47+
['response' => $response, 'request' => $request, 'startedDateTime' => $startedDateTime] = $entry;
48+
49+
$body = $this->getContent($response['content']);
50+
$entryMethod = $request['method'];
51+
$entryUrl = $request['url'];
52+
$requestBody = $options['body'] ?? null;
53+
54+
if ($method !== $entryMethod || $url !== $entryUrl) {
55+
continue;
56+
}
57+
58+
if (null !== $requestBody && $requestBody !== $this->getContent($request['postData'] ?? [])) {
59+
continue;
60+
}
61+
62+
$info = [
63+
'http_code' => $response['status'],
64+
'http_method' => $entryMethod,
65+
'response_headers' => [],
66+
'start_time' => strtotime($startedDateTime),
67+
'url' => $entryUrl,
68+
];
69+
70+
/** @var array{name: string, value: string} $header */
71+
foreach ($response['headers'] as $header) {
72+
['name' => $name, 'value' => $value] = $header;
73+
74+
$info['response_headers'][$name][] = $value;
75+
}
76+
77+
return new MockResponse($body, $info);
78+
}
79+
80+
throw new TransportException(sprintf('File "%s" does not contain a response for HTTP request "%s" "%s".', $this->archiveFile, $method, $url));
81+
}
82+
83+
/**
84+
* @param array{text: string, encoding: string} $content
85+
*/
86+
private function getContent(array $content): string
87+
{
88+
$text = $content['text'] ?? '';
89+
$encoding = $content['encoding'] ?? null;
90+
91+
return match ($encoding) {
92+
'base64' => base64_decode($text),
93+
null => $text,
94+
default => throw new \InvalidArgumentException(sprintf('Unsupported encoding "%s", currently only base64 is supported.', $encoding)),
95+
};
96+
}
97+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.