generated from Nerdware-LLC/template-npm-pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge changes from v1.5.0-next.1 into main (#27)
- Loading branch information
Showing
6 changed files
with
93 additions
and
11 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 +1,88 @@ | ||
import { isValidURL } from "./url.js"; | ||
import { isValidURL, isValidHttpURL } from "./url.js"; | ||
|
||
describe("validate: url", () => { | ||
describe("validate: URL", () => { | ||
// POSITIVE TEST CASES: | ||
|
||
test("returns true when given a valid absolute HTTP/S URL string", () => { | ||
expect(isValidURL("https://example.com")).toBe(true); | ||
expect(isValidURL("https://example.com/")).toBe(true); | ||
expect(isValidURL("https://example.com/foo")).toBe(true); | ||
expect(isValidURL("https://example.com/foo/path?query=value")).toBe(true); | ||
expect(isValidURL("https://example.com/foo/path?query=value#anchor-name")).toBe(true); | ||
}); | ||
test("returns true when given a valid relative URL without a protocol prefix", () => { | ||
expect(isValidURL("example.com")).toBe(true); | ||
expect(isValidURL("example.com/")).toBe(true); | ||
expect(isValidURL("example.com/foo")).toBe(true); | ||
expect(isValidURL("example.com/foo?bar=qux")).toBe(true); | ||
expect(isValidURL("example.com/foo?bar=qux#anchor-name")).toBe(true); | ||
}); | ||
test("returns true when given a valid URL with a protocol other than HTTP/S", () => { | ||
expect(isValidURL("ftp://example.com")).toBe(true); | ||
expect(isValidURL("ftp://example.com/")).toBe(true); | ||
expect(isValidURL("ftp://example.com/foo")).toBe(true); | ||
expect(isValidURL("ftp://example.com/foo?bar=qux")).toBe(true); | ||
expect(isValidURL("ftp://example.com/foo?bar=qux#anchor-name")).toBe(true); | ||
|
||
expect(isValidURL("s3://example.com")).toBe(true); | ||
expect(isValidURL("s3://example.com/")).toBe(true); | ||
expect(isValidURL("s3://example.com/foo")).toBe(true); | ||
expect(isValidURL("s3://example.com/foo?bar=qux")).toBe(true); | ||
expect(isValidURL("s3://example.com/foo?bar=qux#anchor-name")).toBe(true); | ||
}); | ||
|
||
// NEGATIVE TEST CASES: | ||
|
||
test("returns false when given a URL which exceeds the max length", () => { | ||
expect(isValidURL(`https://example.com/${Array(2000).join("x")}`)).toBe(false); | ||
}); | ||
test("returns false when given an absolute HTTP/S URL string which includes a null unicode character", () => { | ||
expect(isValidURL("https://example\u0000.com")).toBe(false); | ||
expect(isValidURL("https://example.com/\u0000")).toBe(false); | ||
expect(isValidURL("https://example.com/foo\u0000")).toBe(false); | ||
expect(isValidURL("https://example.com/foo?bar\u0000=qux")).toBe(false); | ||
expect(isValidURL("https://example.com/foo?bar=qux\u0000")).toBe(false); | ||
expect(isValidURL("https://example.com/foo?bar=qux#\u0000")).toBe(false); | ||
expect(isValidURL("https://example.com/foo?bar=qux#anchor\u0000")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("validate: HTTP URL", () => { | ||
// POSITIVE TEST CASES: | ||
|
||
test("returns true when given a valid absolute HTTP/S URL string", () => { | ||
expect(isValidHttpURL("https://example.com")).toBe(true); | ||
expect(isValidHttpURL("https://example.com/")).toBe(true); | ||
expect(isValidHttpURL("https://example.com/foo")).toBe(true); | ||
expect(isValidHttpURL("https://example.com/foo/path?query=value")).toBe(true); | ||
expect(isValidHttpURL("https://example.com/foo/path?query=value#anchor-name")).toBe(true); | ||
}); | ||
|
||
// NEGATIVE TEST CASES: | ||
|
||
test("returns false when given a URL without a protocol prefix", () => { | ||
expect(isValidURL("example.com/foo")).toBe(false); | ||
expect(isValidHttpURL("example.com")).toBe(false); | ||
expect(isValidHttpURL("example.com/")).toBe(false); | ||
expect(isValidHttpURL("example.com/foo")).toBe(false); | ||
expect(isValidHttpURL("example.com/foo?bar=qux")).toBe(false); | ||
expect(isValidHttpURL("example.com/foo?bar=qux#anchor-name")).toBe(false); | ||
}); | ||
test("returns false when given a URL with a protocol other than HTTP/S", () => { | ||
expect(isValidURL("ftp://example.com/")).toBe(false); | ||
expect(isValidHttpURL("abc://example.com")).toBe(false); | ||
expect(isValidHttpURL("ftp://example.com")).toBe(false); | ||
expect(isValidHttpURL("s3://example.com")).toBe(false); | ||
expect(isValidHttpURL("x://example.com")).toBe(false); | ||
}); | ||
test("returns false when given a URL which exceeds the max length", () => { | ||
expect(isValidURL(`https://example.com/${Array(2000).join("x")}`)).toBe(false); | ||
expect(isValidHttpURL(`https://example.com/${Array(2000).join("x")}`)).toBe(false); | ||
}); | ||
test("returns false when given an absolute HTTP/S URL string which includes a null unicode character", () => { | ||
expect(isValidURL("https://example.com/\u0000")).toBe(false); | ||
expect(isValidHttpURL("https://example\u0000.com")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/\u0000")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/foo\u0000")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/foo?bar\u0000=qux")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/foo?bar=qux\u0000")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/foo?bar=qux#\u0000")).toBe(false); | ||
expect(isValidHttpURL("https://example.com/foo?bar=qux#anchor\u0000")).toBe(false); | ||
}); | ||
}); |
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,8 +1,18 @@ | ||
import { getRegexValidatorFn } from "./_helpers.js"; | ||
|
||
/** | ||
* Returns `true` if `value` is a valid absolute HTTP/S URL. | ||
* Returns `true` if `value` is a valid absolute or relative URL. | ||
* | ||
* > The regex pattern used in this function is _protocol agnostic_ and will match any URL | ||
* > that starts with a valid protocol comprised of 2-17 ASCII alphabetic characters. | ||
*/ | ||
export const isValidURL = getRegexValidatorFn( | ||
/^(([a-zA-Z]{2,17}:\/\/)?[-a-zA-Z0-9@:%._+~#=]{2,256}(\.[a-zA-Z]{2,6})?(:[0-9]+)?\b([-a-zA-Z0-9@:%_+.~#?&//=]{0,1700})?)$/ | ||
); | ||
|
||
/** | ||
* Returns `true` if `value` is a valid absolute http/https URL. | ||
*/ | ||
export const isValidHttpURL = getRegexValidatorFn( | ||
/^(http(s)?:\/\/[-a-zA-Z0-9@:%._+~#=]{2,256}(\.[a-zA-Z]{2,6})?(:[0-9]+)?\b([-a-zA-Z0-9@:%_+.~#?&//=]{0,1700})?)$/ | ||
); |