Skip to content

fix: support wildcard ports in url matching#2677

Merged
kettanaito merged 4 commits intomswjs:mainfrom
trknhr:fix-2204-wildcard-port
Mar 20, 2026
Merged

fix: support wildcard ports in url matching#2677
kettanaito merged 4 commits intomswjs:mainfrom
trknhr:fix-2204-wildcard-port

Conversation

@trknhr
Copy link
Copy Markdown
Contributor

@trknhr trknhr commented Mar 19, 2026

Support absolute URLs that use wildcard ports in request matching.

MSW already coerces request masks before passing them to path-to-regexp. Numeric ports were escaped there, but wildcard ports like http://localhost:* were left as :(.*), which path-to-regexp interprets as an invalid parameter declaration and throws.

This updates coercePath() to escape the port separator when the port is either numeric or a wildcard-expanded capture group. It also adds regression tests for HTTP and WebSocket URLs, including a pathname case.

This keeps the current path-to-regexp-based matching behavior intact instead of broadening the fix into a larger matcher change.

Fixes #2204

trknhr and others added 2 commits March 19, 2026 22:42
Escape wildcard port separators in coercePath so absolute URLs like http://localhost:* and ws://localhost:* match without throwing. Add regression coverage for HTTP and WebSocket patterns, including a pathname case.

Fixes mswjs#2204
@kettanaito
Copy link
Copy Markdown
Member

Hi, @trknhr. Thanks for opening this!

I've added a few missing test cases and they are failing at the moment. I don't believe your suggestion has truly implemented the behavior that the users expect. I might take a look at this once I have a moment. Hopefully, shouldn't be a big change.

@kettanaito kettanaito mentioned this pull request Mar 19, 2026
2 tasks
@kettanaito kettanaito marked this pull request as ready for review March 20, 2026 17:39
@kettanaito kettanaito changed the title fix(core): support wildcard ports in url matching fix: support wildcard ports in url matching Mar 20, 2026
Copy link
Copy Markdown
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 good and the expectations align with #2678.

@kettanaito
Copy link
Copy Markdown
Member

Thank you for fixing the issue! 👏

@kettanaito kettanaito merged commit f90bf49 into mswjs:main Mar 20, 2026
18 checks passed
@kettanaito
Copy link
Copy Markdown
Member

Released: v2.12.14 🎉

This has been released in v2.12.14.

Get these changes by running the following command:

npm i msw@latest

Predictable release automation by 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.

Using the wildcard token to try to match random ports doesn't work if preceded by a colon

2 participants