Skip to content

[HttpClient] Add an HAR response factory for testing #50807

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
Jul 9, 2023

Conversation

GaryPEGEOT
Copy link
Contributor

@GaryPEGEOT GaryPEGEOT commented Jun 28, 2023

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:

// 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();

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

If the HAR file can contain multiple request/responses. Then I suggest yielding. That means we can do:

$factory = new HarFileResponseFactory($myHarFile);
$client = new MockHttpClient($factory);

$json = json_decode(
json: file_get_contents($this->archiveFile),
associative: true,
flags: \JSON_THROW_ON_ERROR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flags: \JSON_THROW_ON_ERROR
flags: \JSON_THROW_ON_ERROR,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be consistent with the code base and use the "short" version here on one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@fabpot fabpot modified the milestones: 6.4, 7.0 Jul 9, 2023
@fabpot
Copy link
Member

fabpot commented Jul 9, 2023

Can you rebase on 7.0 and add a note in the CHANGELOG?

@fabpot
Copy link
Member

fabpot commented Jul 9, 2023

Thank you @GaryPEGEOT.

@fabpot fabpot force-pushed the feat/har-response-factory branch from bb758cc to 01d2e35 Compare July 9, 2023 11:12
@fabpot fabpot merged commit c63faff into symfony:7.0 Jul 9, 2023
@GaryPEGEOT GaryPEGEOT deleted the feat/har-response-factory branch July 9, 2023 11:16
@nicolas-grekas nicolas-grekas modified the milestones: 7.0, 6.4 Jul 20, 2023
@fabpot fabpot mentioned this pull request Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants