question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot match absolute URLs with port number

See original GitHub issue

Describe the bug

Recently in version 0.36.0 path-to-regexp was used over node-match-path to have similar matching behaviour as react-router and express. However, path-to-regexp doesn’t seem to match absolute URLs as node-match-path does. It feels that using path-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

  1. Define a handler with an absolute url such as rest.get('http://localhost:1234/*')
  2. Execute a request against http://localhost:1234/foo/bar

Example 2

  1. Define a handler with an absolute url such as rest.get('/:firstSegment/:secondSegment')
  2. Execute a request against http://localhost:1234/foo/bar

Example 3

  1. Define a handler with an absolute url such as rest.get('http\\://localhost\\:3456/:project/:application')
  2. Execute a request against 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 that path-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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Dec 12, 2021

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:

https://github.com/mswjs/msw/blob/d5b7312969ede03031d429c9fdd38a4d783710d1/src/utils/matching/matchRequestUrl.ts#L17-L46

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?

1reaction
tdeekenscommented, Dec 12, 2021

Just learning that msw tries to coerce the path to a path-to-regexp compatible version e.g. converting a http://localhost:3456/*" to a "http\\://localhost:3456/(.*). Still unsure why my handler stopped matching.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relative URL to a different port number in a hyperlink?
This script will take urls that look like this: [given current url: 'http://some-domain.com:3000/a/path/file.html'] 1. <a data-samehost-port=" ...
Read more >
Documentation/UX feedback regarding relative URL matching
Note that relative URL are resolved against the current location ( location. origin ). And so request matching in MSW works the same...
Read more >
Why you should (almost) never use an absolute path to your ...
It specifies that all requests which match the location block — in this case, /api path — should be forwarded to http://api.appfocused.com , ......
Read more >
2.6 URLs — HTML5 - W3C
The URL is a valid IRI reference and it has no query component. [RFC3987] ... The substring matched by the <port> production, if...
Read more >
How to handle relative urls correctly with a reverse proxy
One) Rewrite the internal app to use relative paths instead of absolute. i.e. . ... For external users the public record for test.example.com...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found