-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
ref(core): Improve URL parsing utilities #15882
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f570a5f
ref(core): Improve URL parsing utilities
AbhiPrasad 0b91b93
make sure we include href
AbhiPrasad 8cc57de
use url sanitization helper
AbhiPrasad a2d3ae6
make sure we only set if defined
AbhiPrasad 1abc4dd
trailing slash
AbhiPrasad 5bf0c6c
Merge branch 'develop' into abhi-ref-parse-string-to-url
AbhiPrasad e389f19
Adjust size-limit
AbhiPrasad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { getSanitizedUrlString, parseStringToURL, parseUrl, stripUrlQueryAndFragment } from '../../src/utils-hoist/url'; | ||
import { | ||
getSanitizedUrlString, | ||
parseUrl, | ||
stripUrlQueryAndFragment, | ||
parseStringToURLObject, | ||
isURLObjectRelative, | ||
getSanitizedUrlStringFromUrlObject, | ||
} from '../../src/utils-hoist/url'; | ||
|
||
describe('stripQueryStringAndFragment', () => { | ||
const urlString = 'http://dogs.are.great:1231/yay/'; | ||
|
@@ -62,8 +69,6 @@ describe('getSanitizedUrlString', () => { | |
'https://[filtered]:[filtered]@somedomain.com', | ||
], | ||
['same-origin url', '/api/v4/users?id=123', '/api/v4/users'], | ||
['url without a protocol', 'example.com', 'example.com'], | ||
['url without a protocol with a path', 'example.com/sub/path?id=123', 'example.com/sub/path'], | ||
['url with port 8080', 'http://172.31.12.144:8080/test', 'http://172.31.12.144:8080/test'], | ||
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'], | ||
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'], | ||
|
@@ -197,19 +202,95 @@ describe('parseUrl', () => { | |
}); | ||
}); | ||
|
||
describe('parseStringToURL', () => { | ||
describe('parseStringToURLObject', () => { | ||
it('returns undefined for invalid URLs', () => { | ||
expect(parseStringToURL('invalid-url')).toBeUndefined(); | ||
expect(parseStringToURLObject('invalid-url')).toBeUndefined(); | ||
}); | ||
|
||
it('returns a URL object for valid URLs', () => { | ||
expect(parseStringToURL('https://somedomain.com')).toBeInstanceOf(URL); | ||
expect(parseStringToURLObject('https://somedomain.com')).toBeInstanceOf(URL); | ||
}); | ||
|
||
it('returns a URL object for valid URLs with a base URL', () => { | ||
expect(parseStringToURLObject('https://somedomain.com', 'https://base.com')).toBeInstanceOf(URL); | ||
}); | ||
|
||
it('returns a relative URL object for relative URLs', () => { | ||
expect(parseStringToURLObject('/path/to/happiness')).toEqual({ | ||
isRelative: true, | ||
pathname: '/path/to/happiness', | ||
search: '', | ||
hash: '', | ||
}); | ||
}); | ||
|
||
it('does not throw an error if URl.canParse is not defined', () => { | ||
const canParse = (URL as any).canParse; | ||
delete (URL as any).canParse; | ||
expect(parseStringToURL('https://somedomain.com')).toBeInstanceOf(URL); | ||
expect(parseStringToURLObject('https://somedomain.com')).toBeInstanceOf(URL); | ||
(URL as any).canParse = canParse; | ||
}); | ||
}); | ||
|
||
describe('isURLObjectRelative', () => { | ||
it('returns true for relative URLs', () => { | ||
expect(isURLObjectRelative(parseStringToURLObject('/path/to/happiness')!)).toBe(true); | ||
}); | ||
|
||
it('returns false for absolute URLs', () => { | ||
expect(isURLObjectRelative(parseStringToURLObject('https://somedomain.com')!)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getSanitizedUrlStringFromUrlObject', () => { | ||
it.each([ | ||
['regular url', 'https://somedomain.com', 'https://somedomain.com/'], | ||
['regular url with a path', 'https://somedomain.com/path/to/happiness', 'https://somedomain.com/path/to/happiness'], | ||
[ | ||
'url with standard http port 80', | ||
'http://somedomain.com:80/path/to/happiness', | ||
'http://somedomain.com/path/to/happiness', | ||
], | ||
[ | ||
'url with standard https port 443', | ||
'https://somedomain.com:443/path/to/happiness', | ||
'https://somedomain.com/path/to/happiness', | ||
], | ||
[ | ||
'url with non-standard port', | ||
'https://somedomain.com:4200/path/to/happiness', | ||
'https://somedomain.com:4200/path/to/happiness', | ||
], | ||
[ | ||
'url with query params', | ||
'https://somedomain.com:4200/path/to/happiness?auhtToken=abc123¶m2=bar', | ||
'https://somedomain.com:4200/path/to/happiness', | ||
], | ||
[ | ||
'url with a fragment', | ||
'https://somedomain.com/path/to/happiness#somewildfragment123', | ||
'https://somedomain.com/path/to/happiness', | ||
], | ||
[ | ||
'url with a fragment and query params', | ||
'https://somedomain.com/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar', | ||
'https://somedomain.com/path/to/happiness', | ||
], | ||
[ | ||
'url with authorization', | ||
'https://username:[email protected]', | ||
'https://%filtered%:%filtered%@somedomain.com/', | ||
], | ||
['same-origin url', '/api/v4/users?id=123', '/api/v4/users'], | ||
['url with port 8080', 'http://172.31.12.144:8080/test', 'http://172.31.12.144:8080/test'], | ||
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'], | ||
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'], | ||
['url with IP and port 80', 'http://172.31.12.144:80/test', 'http://172.31.12.144/test'], | ||
])('returns a sanitized URL for a %s', (_, rawUrl: string, sanitizedURL: string) => { | ||
const urlObject = parseStringToURLObject(rawUrl); | ||
if (!urlObject) { | ||
throw new Error('Invalid URL'); | ||
} | ||
expect(getSanitizedUrlStringFromUrlObject(urlObject)).toEqual(sanitizedURL); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dayum