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

fix: preserve search params in "onUnhandledRequest" messages #2128

Merged

Conversation

nagadevkrishna
Copy link
Contributor

@nagadevkrishna nagadevkrishna commented Apr 9, 2024

@@ -9,7 +9,9 @@ export function toPublicUrl(url: string | URL): string {

const urlInstance = url instanceof URL ? url : new URL(url)

const [, relativeUrl] = urlInstance.href.split(urlInstance.origin)
Copy link
Member

@kettanaito kettanaito Apr 10, 2024

Choose a reason for hiding this comment

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

This does solve the problem but it changes the other use cases we have for toPublicUrl.

I think a far better approach is to keep these changes scoped to the immediate problem area: the toPublic() usage in the onUnhandledRequest.ts.

const url = new URL(request.url)
const publicUrl = toPublicUrl(url)
const unhandledRequestMessage = `intercepted a request without a matching request handler:\n\n \u2022 ${request.method} ${publicUrl}\n\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks`

Let's try something like this:

const url = new URL(request.url)
const publicUrl = toPublicUrl(url) + url.search

This way both relative and absolute URLs will be printed correctly but will also have any search parameters preserved.

Then, we can modify the tests in onUnhandledRequest.test.ts to include two new scenarios:

  • Unhandled request with a relative URL and search params;
  • Unhandled request with an absolute URL and search params.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Made changes now and pushed to PR again.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this, @nagadevkrishna! I left a suggestion, let me know what you think about it.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

This looks fantastic!

@kettanaito kettanaito changed the title fix: preserving qparams in url if origin is same, which prints relative url in warning message fix: preserve search params in "onUnhandledRequest" messages Apr 12, 2024
@nagadevkrishna
Copy link
Contributor Author

awesome!

@kettanaito
Copy link
Member

Don't close it just yet. I need to merge it so it gets to MSW. Be patient.

@kettanaito kettanaito merged commit 64bcae7 into mswjs:main Apr 17, 2024
11 checks passed
@kettanaito
Copy link
Member

Welcome to the contributors, @nagadevkrishna! 👏

@kettanaito
Copy link
Member

Released: v2.2.14 🎉

This has been released in v2.2.14!

Make sure to always update to the latest version (npm i msw@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve the original request URL in "onUnhandledRequest" warnings
2 participants