Skip to content
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

HAR: Respect query parameters #45

Closed
kettanaito opened this issue Jul 9, 2024 · 4 comments · Fixed by #46
Closed

HAR: Respect query parameters #45

kettanaito opened this issue Jul 9, 2024 · 4 comments · Fixed by #46
Assignees
Labels
enhancement New feature or request scope:har

Comments

@kettanaito
Copy link
Member

kettanaito commented Jul 9, 2024

MSW removes query parameters from request URLs. We need to take those into account during the request handler generation. Most likely, to create a custom predicate with a order-insensitive match for the query parameters.

Current behavior

If the recorded traffic contains requests with query parameters, those will be stripped away by MSW (intended behavior). However, they must be respected in order to match the right requests in the generated handlers.

Expected behavior

If the recorded request has query parameters, those are translated into a predicate function within the response resolver that only matches the same requests in tests/runtime.

Note: Query parameters order should not matter.

// someplace/in/the/generator.js

const recordedRequestUrl = new URL(entry.request.url)
new HttpHandler(method, url, ({ request }) => {
  // If the recorded request has query parameters and the 
  // intercepted request doesn't match them all, skip it. 
  if (recordedRequestUrl.search && !matchesQueryParameters(request, recordedRequestUrl.searchParams)) {
    return
  }
})
@weyert
Copy link
Collaborator

weyert commented Jul 9, 2024

That's a good one!

@kettanaito
Copy link
Member Author

This is somewhat related to mswjs/msw#1804. If the mentioned API is implemented on MSW's side, we should use it for query parameter predicates. The idea behind a new API is to allow the developer to introduce request predicates before the parsing phase of the request handler. This yields better performance.

But we shouldn't wait for that one to land. We can implement the predicate within the response resolver for now, that will also work.

@weyert, please, would you be interested in tackling this task? I will help with the code reviews and seeing it released. Let me know!

@weyert
Copy link
Collaborator

weyert commented Jul 9, 2024

@weyert, please, would you be interested in tackling this task? I will help with the code reviews and seeing it released. Let me know!

Sure, I can have a look in the coming days. I have to complete some work stuff first.

@kettanaito
Copy link
Member Author

Released: v0.2.0 🎉

This has been released in v0.2.0!

Make sure to always update to the latest version (npm i @mswjs/source@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request scope:har
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants