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
Cannot match absolute URLs with port number #1027
Comments
Just learning that I just assumed it wouldn't do that when debugging and ended up with double escaping. Update: I assume cause the port is not escaped. |
Still even with const { match } = require("path-to-regexp");
console.log(match("http\\://localhost\\:3456/(.*)")('http://localhost:3456/foo/bar')) Returning { path: "http://localhost:3456/foo/bar", index: 0, params: Object {0: "foo/bar"} } |
Resolved the issue. It was the unescaped port. So one needs a handler as in All of which is quite hard to debug and know. I only learned by reading code (which is always good) and debugging Hope this helps somebody after me. |
Hey, @tdeekens. Thanks for reporting this. I still treat this as an issue on our side. The library should escape the port the same as it automatically escapes the protocol. I believe we can achieve that by changing the logic of this coercing function: msw/src/utils/matching/matchRequestUrl.ts Lines 17 to 46 in d5b7312
You can see that it escapes the protocol already. We can add another regular expression to handle the port number. Would you be interested in opening a pull request with this fix? |
path-to-regexp
breaks matching absolute urls
Hi @kettanaito, sure. I however don't really feel good about fiddling with URLs using Regular Expressions. There are so many edge cases we might not think of. Like URLs which are IPs or contain A first attempt at a minimal RegExp which aren't my strong suit 😄 would be |
Describe the bug
Recently in version
0.36.0
path-to-regexp
was used overnode-match-path
to have similar matching behaviour asreact-router
andexpress
. However,path-to-regexp
doesn't seem to match absolute URLs asnode-match-path
does. It feels that usingpath-to-regexp
is a super valuable step forward but can more easily break mocks than anticipated.Environment
msw: 0.36.3
nodejs: 17.x.x
To Reproduce
Example 1
rest.get('http://localhost:1234/*')
http://localhost:1234/foo/bar
Example 2
rest.get('/:firstSegment/:secondSegment')
http://localhost:1234/foo/bar
Example 3
rest.get('http\\://localhost\\:3456/:project/:application')
http://localhost:1234/foo/bar
In all examples nothing will be matched or an error will be thrown.
Expected behavior
The URL to be matched or improved documentation how to deal with absolute URLs. It seems that
path-to-regexp
requires some escaping while not working with "catch-all" handers as well. Given thatpath-to-regexp
will continue to be used a few more practical examples for migrating handlers with full urls could help the community if I am not the only one having this issue.The text was updated successfully, but these errors were encountered: