77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { buildDnsRecord } from "../../src/network/dns";
|
|
import { buildUrl, inspectUrl } from "../../src/network/url";
|
|
|
|
describe("URL inspection", () => {
|
|
it("preserves duplicate query values and resolves relative URLs", () => {
|
|
const inspected = inspectUrl(
|
|
"../items?a=1&a=2#part",
|
|
"https://Example.com/a/b/",
|
|
);
|
|
expect(inspected.hostname).toBe("example.com");
|
|
expect(inspected.query).toEqual([
|
|
{ key: "a", value: "1" },
|
|
{ key: "a", value: "2" },
|
|
]);
|
|
expect(buildUrl(inspected)).toBe(
|
|
"https://example.com/a/items?a=1&a=2#part",
|
|
);
|
|
});
|
|
|
|
it("warns about credentials without disclosing the password", () => {
|
|
const inspected = inspectUrl("https://alice:secret@example.test/");
|
|
expect(inspected.hasPassword).toBe(true);
|
|
expect(JSON.stringify(inspected)).not.toContain("secret");
|
|
});
|
|
});
|
|
|
|
describe("DNS construction", () => {
|
|
it("builds common zone-file records", () => {
|
|
expect(
|
|
buildDnsRecord({
|
|
owner: "www",
|
|
ttl: 3600,
|
|
type: "A",
|
|
value: "192.0.2.4",
|
|
}),
|
|
).toBe("www 3600 IN A 192.0.2.4");
|
|
expect(
|
|
buildDnsRecord({
|
|
owner: "@",
|
|
ttl: 300,
|
|
type: "MX",
|
|
value: "mail.example.",
|
|
priority: 10,
|
|
}),
|
|
).toBe("@ 300 IN MX 10 mail.example.");
|
|
});
|
|
|
|
it("quotes TXT data and rejects oversized values", () => {
|
|
expect(
|
|
buildDnsRecord({ owner: "@", ttl: 60, type: "TXT", value: 'a"b' }),
|
|
).toContain('"a\\"b"');
|
|
expect(() =>
|
|
buildDnsRecord({
|
|
owner: "@",
|
|
ttl: 60,
|
|
type: "TXT",
|
|
value: "x".repeat(256),
|
|
}),
|
|
).toThrow(/255/u);
|
|
});
|
|
|
|
it("canonicalises addresses and rejects malformed IPv6 text", () => {
|
|
expect(
|
|
buildDnsRecord({
|
|
owner: "@",
|
|
ttl: 60,
|
|
type: "AAAA",
|
|
value: "2001:0db8:0:0:0:0:0:1",
|
|
}),
|
|
).toBe("@ 60 IN AAAA 2001:db8::1");
|
|
expect(() =>
|
|
buildDnsRecord({ owner: "@", ttl: 60, type: "AAAA", value: "::::" }),
|
|
).toThrow(/IPv6/u);
|
|
});
|
|
});
|