chore: 补充单元测试

This commit is contained in:
xiaojunnuo
2026-05-01 09:16:46 +08:00
parent 80092823db
commit 0a0f1e90e1
24 changed files with 656 additions and 187 deletions
@@ -0,0 +1,43 @@
/// <reference types="mocha" />
import { expect } from "chai";
import { domainUtils } from "./util.domain.js";
describe("domainUtils", () => {
describe("match", () => {
it("matches exact domains", () => {
expect(domainUtils.match("example.com", ["example.com"])).to.equal(true);
expect(domainUtils.match("api.example.com", ["example.com"])).to.equal(false);
});
it("matches wildcard domains by suffix", () => {
expect(domainUtils.match("api.example.com", ["*.example.com"])).to.equal(true);
expect(domainUtils.match("deep.api.example.com", ["*.example.com"])).to.equal(false);
expect(domainUtils.match("example.com", ["*.example.com"])).to.equal(false);
});
it("requires every target domain to match", () => {
expect(domainUtils.match(["api.example.com", "admin.example.com"], ["*.example.com"])).to.equal(true);
expect(domainUtils.match(["api.example.com", "other.com"], ["*.example.com"])).to.equal(false);
});
});
describe("isIp", () => {
it("detects valid IPv4 addresses", () => {
expect(domainUtils.isIpv4("127.0.0.1")).to.equal(true);
expect(domainUtils.isIpv4("255.255.255.255")).to.equal(true);
});
it("rejects invalid IPv4 addresses", () => {
expect(domainUtils.isIpv4("999.1.1.1")).to.equal(false);
expect(domainUtils.isIpv4("1.2.3")).to.equal(false);
expect(domainUtils.isIpv4("example.com")).to.equal(false);
});
it("detects IPv6 addresses", () => {
expect(domainUtils.isIpv6("2001:db8::1")).to.equal(true);
expect(domainUtils.isIp("2001:db8::1")).to.equal(true);
});
});
});