imporve: test for validLink

This commit is contained in:
Alikia2x 2024-03-31 17:06:05 +08:00
parent 24de7bcaa3
commit c1930d6bd7
3 changed files with 66 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,7 @@ export default function validLink(link: string) {
try {
const url = new URL(link);
finalURL = url.origin;
return true;
} catch (error) {
// if the URL is invalid, try to add the protocol
try {
@ -22,7 +23,7 @@ export default function validLink(link: string) {
}
}
function validTLD(domain: string): boolean {
export function validTLD(domain: string): boolean {
const tld = punycode.toUnicode(domain.split(".").reverse()[0]);
if (tldList.includes(tld)) {
return true;

View File

@ -1,6 +1,64 @@
import {describe, expect, test} from '@jest/globals';
import validLink from "../lib/url/validLink";
import { describe, expect, test } from "@jest/globals";
import validLink, { validTLD } from "../lib/url/validLink";
test("Plain, full URL", () => {
expect(validLink("https://example.com/about-us")).toBe(true);
});
describe("Check if a string is an accessible domain/URL", () => {
test("Plain, full URL", () => {
// Plain form
expect(validLink("http://example.com")).toBe(true);
// With https and path
expect(validLink("https://jestjs.io/docs/getting-started/")).toBe(true);
// With anchor
expect(validLink("https://difftastic.wilfred.me.uk/zh-CN/git.html#git-difftool")).toBe(true);
// With params
expect(validLink("https://www.bilibili.com/list/ml2252204359?oid=990610203&bvid=BV1sx4y1g7Hh")).toBe(true);
});
test("Punycode URL", () => {
expect(validLink("https://原神大学.com/")).toBe(true);
expect(validLink("中国原神大学.com")).toBe(true);
});
test("Invalid TLD with protocol", () => {
expect(validLink("https://www.example.notexist")).toBe(true);
});
test("Invalid TLD with no protocol", () => {
expect(validLink("www.example.notexist")).toBe(false);
});
});
// Reference: https://www.iana.org/domains/root/db
describe("Check if the given TLD exist and assigned.", () => {
test("Valid normal TLD", () => {
expect(validTLD("com")).toBe(true);
expect(validTLD("top")).toBe(true);
expect(validTLD("net")).toBe(true);
expect(validTLD("org")).toBe(true);
});
test("Valid new TLDs", () => {
// they really exist!
expect(validTLD("foo")).toBe(true);
expect(validTLD("bar")).toBe(true);
});
test("Exist but not assigned TLD", () => {
expect(validTLD("active")).toBe(false);
expect(validTLD("off")).toBe(false);
});
test("with dot", () => {
expect(validTLD(".com")).toBe(true);
expect(validTLD(".us")).toBe(true);
expect(validTLD(".cn")).toBe(true);
expect(validTLD(".io")).toBe(true);
});
test("Punycode TLDs", () => {
expect(validTLD(".中国")).toBe(true);
expect(validTLD(".РФ")).toBe(true);
expect(validTLD(".कॉम")).toBe(true);
expect(validTLD("ایران")).toBe(true);
expect(validTLD("இலங்கை")).toBe(true);
expect(validTLD("გე")).toBe(true);
expect(validTLD("ポイント")).toBe(true);
});
test("Punycode TLDs but not assigned", () => {
expect(validTLD("テスト")).toBe(false);
expect(validTLD("परीक्षा")).toBe(false);
expect(validTLD("测试")).toBe(false);
});
});