-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from allroundexperts/fix-17205
fix: added protocols to the url along with correct port range
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {URL_REGEX_WITH_REQUIRED_PROTOCOL, URL_REGEX} from '../lib/Url'; | ||
|
||
describe('Mandatory protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('http://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535/path/my')).toBeTruthy(); | ||
}); | ||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('google.com')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:65536')).toBeFalsy(); | ||
expect(regexToTest.test('smtp://google.com')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('Optional protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535/path/my')).toBeTruthy(); | ||
}); | ||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('google.com:65536')).toBeFalsy(); | ||
}); | ||
}); |
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,16 +1,22 @@ | ||
import TLD_REGEX from './tlds'; | ||
|
||
const URL_WEBSITE_REGEX = `(https?:\\/\\/)?((?:www\\.)?[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)+(?:${TLD_REGEX})(?:\\:\\d{2,4}|\\b|(?=_))`; | ||
const ALLOWED_PORTS = '([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])'; | ||
const URL_PROTOCOL_REGEX = '((ht|f)tps?:\\/\\/)'; | ||
const URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}?((?:www\\.)?[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)+(?:${TLD_REGEX})(?:\\:${ALLOWED_PORTS}|\\b|(?=_))`; | ||
const addEscapedChar = reg => `(?:${reg}|&(?:amp|quot|#x27);)`; | ||
const URL_PATH_REGEX = `(?:${addEscapedChar('[.,=(+$!*]')}?\\/${addEscapedChar('[-\\w$@.+!*:(),=%~]')}*${addEscapedChar('[-\\w~@:%)]')}|\\/)*`; | ||
const URL_PARAM_REGEX = `(?:\\?${addEscapedChar('[-\\w$@.+!*()\\/,=%{}:;\\[\\]\\|_]')}*)?`; | ||
const URL_FRAGMENT_REGEX = `(?:#${addEscapedChar('[-\\w$@.+!*()[\\],=%;\\/:~]')}*)?`; | ||
const URL_REGEX = `(${URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`; | ||
|
||
const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX); | ||
|
||
export { | ||
URL_WEBSITE_REGEX, | ||
URL_PATH_REGEX, | ||
URL_PARAM_REGEX, | ||
URL_FRAGMENT_REGEX, | ||
URL_REGEX | ||
URL_REGEX, | ||
URL_REGEX_WITH_REQUIRED_PROTOCOL, | ||
URL_PROTOCOL_REGEX, | ||
}; |