-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repo): github profile url should be valid (#268)
* feat(repo): github profile url should be valid * docs: add k3nsei as a contributor
- Loading branch information
Showing
6 changed files
with
100 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import url from '../url'; | ||
|
||
test(`Result of protocol validation should be true`, () => { | ||
expect(url.isHttpProtocol('http:')).toBe(true) | ||
expect(url.isHttpProtocol('https:')).toBe(true) | ||
}) | ||
|
||
test(`Result of protocol validation should be false`, () => { | ||
expect(url.isHttpProtocol('ftp:')).toBe(false) | ||
}) | ||
|
||
test(`Result of url validation should be true`, () => { | ||
expect(url.isValidHttpUrl('https://api.github.com/users/octocat')).toBe(true) | ||
}) | ||
|
||
test(`Result of url validation should be false when url uses wrong protocol`, () => { | ||
expect(url.isValidHttpUrl('git://[email protected]:all-contributors/all-contributors-cli.git')).toBe(false) | ||
}) | ||
|
||
test(`Result of url validation should be false when input isn't url`, () => { | ||
expect(url.isValidHttpUrl('github-octocat')).toBe(false) | ||
}) | ||
|
||
test(`Result of parsed url should be equal`, () => { | ||
const input = 'https://api.github.com/users/octocat' | ||
const expected = 'https://api.github.com/users/octocat' | ||
expect(url.parseHttpUrl(input)).toBe(expected) | ||
}) | ||
|
||
test(`Result of parsed url without protocol should be equal`, () => { | ||
const input = 'example.com' | ||
const expected = 'http://example.com/' | ||
expect(url.parseHttpUrl(input)).toBe(expected) | ||
}) | ||
|
||
test(`Throw an error when parsed input isn't a string`, () => { | ||
const input = 123 | ||
expect(url.parseHttpUrl.bind(null, input)).toThrowError('input must be a string') | ||
}) | ||
|
||
test(`Throw an error when parsed url has wrong protocol`, () => { | ||
const input = 'ftp://domain.xyz' | ||
expect(url.parseHttpUrl.bind(null, input)).toThrowError('Provided URL has an invalid protocol') | ||
}) | ||
|
||
test(`Throw an error when parsed input isn't a URL`, () => { | ||
const input = 'some string' | ||
expect(url.parseHttpUrl.bind(null, input)).toThrowError('Invalid URL: http://some string') | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function isHttpProtocol(input) { | ||
return new RegExp('^https?\\:?$').test(input) | ||
} | ||
|
||
function isValidHttpUrl(input) { | ||
try { | ||
const url = new URL(input) | ||
|
||
return isHttpProtocol(url.protocol) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
function parseHttpUrl(input) { | ||
if (typeof input !== 'string') { | ||
throw new TypeError('input must be a string') | ||
} | ||
|
||
const url = new URL(new RegExp('^\\w+\\:\\/\\/').test(input) ? input : `http://${input}`) | ||
|
||
if (!isHttpProtocol(url.protocol)) { | ||
throw new TypeError('Provided URL has an invalid protocol') | ||
} | ||
|
||
return url.toString() | ||
} | ||
|
||
module.exports = { | ||
isHttpProtocol, | ||
isValidHttpUrl, | ||
parseHttpUrl | ||
} |