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

Passed query parameters are completely ignored #2385

Closed
oliversalzburg opened this issue Oct 27, 2023 · 4 comments
Closed

Passed query parameters are completely ignored #2385

oliversalzburg opened this issue Oct 27, 2023 · 4 comments
Labels
bug Something isn't working Docs Changes related to the documentation

Comments

@oliversalzburg
Copy link
Contributor

Bug Description

When you pass a query object to intercept() it is only respected if the mocked path is of type string, otherwise the entire query config is silently discarded:

if (typeof opts.path === 'string') {
if (opts.query) {
opts.path = buildURL(opts.path, opts.query)
} else {
// Matches https://github.com/nodejs/undici/blob/main/lib/fetch/index.js#L1811
const parsedURL = new URL(opts.path, 'data://')
opts.path = parsedURL.pathname + parsedURL.search
}
}

Thus, setting query in these cases has no effect and all requests will be captured, regardless of their search parameters.

Reproducible By

Expected Behavior

Only requests that are matched by the query object are mocked. Ideally, query would accept a callback function, just like the other options.

Logs & Screenshots

Environment

Debian 12, Node 20

Additional context

@oliversalzburg oliversalzburg added the bug Something isn't working label Oct 27, 2023
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@oliversalzburg
Copy link
Contributor Author

I wouldn't mind, but I think I was mostly confusing myself, because I was trying to port some existing nock code.

Now, I just handle path matching with a callback. This callback receives the path with the potential query attached, so I can just .split("?") it off, parse, and apply any kind of matching on it that I want. So the query option in its current form seems to be soley sugar for buildUrl when providing a string URL and the query as a plain hash. As this is the only case where the query option is used, it feels a bit redundant to me. I feel like consumers should just call buildUrl themselves if they want to construct a path that includes query parameters, and then just have a single location (the path matching callback) to handle the matching consistently.

That seems reasonable, compared to implementing an entire new code path for query parameter matching in undici. It would also raise the question if consumers should even receive query parameters as part of the path in the future, as there would be a dedicated path for query matching then. That also sounds like a breaking change that's probably not worth it.

So, maybe just a small addition to the documentation to mention that the query option only applies to string URLs and that the path, when supplied as a RegExp or a callback, will receive the path with query parameters attached as input? Because I think that was just unclear to me.

Let me know what you think 😊

@metcoder95
Copy link
Member

Would you like to send a PR for it?

@metcoder95 metcoder95 added the Docs Changes related to the documentation label Nov 1, 2023
@oliversalzburg
Copy link
Contributor Author

Yeah, I'll try to make it happen this weekend :)

metcoder95 pushed a commit that referenced this issue Nov 22, 2023
The behavior of `query` was unclear, as it only applies to the case where `path` is provided as a `string`.

It also wasn't clear that the `RegExp` and callback case will receive the query as part of the path.

The additional guidance should help with both. To further clarify the use case, an example for the callback scenario was added.

Closes: #2385
crysmags pushed a commit to crysmags/undici that referenced this issue Feb 27, 2024
The behavior of `query` was unclear, as it only applies to the case where `path` is provided as a `string`.

It also wasn't clear that the `RegExp` and callback case will receive the query as part of the path.

The additional guidance should help with both. To further clarify the use case, an example for the callback scenario was added.

Closes: nodejs#2385
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Docs Changes related to the documentation
Projects
None yet
Development

No branches or pull requests

3 participants