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

Fixes escaping port number in absolute urls #1028

Merged
merged 2 commits into from Dec 14, 2021

Conversation

tdeekens
Copy link
Contributor

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 13, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4563b6c:

Sandbox Source
MSW React Configuration

* Escape the port so that "path-to-regexp" can match
* absolute URLs including port numbers.
*/
.replace(/((?::)(?:[0-9]+))/g, '\\$1')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively the following should also work /(:[0-9]+)/. @kettanaito you can pick :)

@@ -104,5 +104,8 @@ describe('coercePath', () => {
expect(coercePath('https://example.com:1234/:userId')).toEqual(
'https\\://example.com\\:1234/:userId',
)
expect(coercePath('https://example.com:1234/:5678')).toEqual(
'https\\://example.com\\:1234/:5678',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how to deal with this case. Suggestions welcome.

@tdeekens
Copy link
Contributor Author

An alternative approach, not using Regular Expressions, could be to use new URL() in a try {} catch() {}. Then one could attempt to build a url if possible and escape as needed and if a URL can't be constructed default to standard escaping e.g. only wildcard syntax.

@kettanaito
Copy link
Member

Thanks for opening this, @tdeekens!

I've adjusted the regular expression to match the port number pattern while allowing leading numbers in the path parameter names (as well as parameter names represented by numbers only). I've also changed the order in which the port is escaped, so it happens before escaping the protocol.

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.

Thank you for helping fix this 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot match absolute URLs with port number
2 participants