Files
caddy-proxy-manager/tests/unit/proxy-host-domains.test.ts
akanealw 99819b70ff
Some checks failed
Build and Push Docker Images (Trusted) / build-and-push (., docker/caddy/Dockerfile, caddy) (push) Has been cancelled
Build and Push Docker Images (Trusted) / build-and-push (., docker/l4-port-manager/Dockerfile, l4-port-manager) (push) Has been cancelled
Build and Push Docker Images (Trusted) / build-and-push (., docker/web/Dockerfile, web) (push) Has been cancelled
Tests / test (push) Has been cancelled
added caddy-proxy-manager for testing
2026-04-21 22:49:08 +00:00

40 lines
1.5 KiB
TypeScript
Executable File

import { describe, expect, it } from "vitest";
import { isValidProxyHostDomain, normalizeProxyHostDomains } from "@/src/lib/proxy-host-domains";
describe("isValidProxyHostDomain", () => {
it("accepts standard hostnames", () => {
expect(isValidProxyHostDomain("app.example.com")).toBe(true);
expect(isValidProxyHostDomain("localhost")).toBe(true);
});
it("accepts wildcard hostnames on the left-most label", () => {
expect(isValidProxyHostDomain("*.example.com")).toBe(true);
expect(isValidProxyHostDomain("*.local")).toBe(true);
});
it("rejects wildcard hostnames outside the left-most label", () => {
expect(isValidProxyHostDomain("api.*.example.com")).toBe(false);
expect(isValidProxyHostDomain("*.*.example.com")).toBe(false);
expect(isValidProxyHostDomain("example.*")).toBe(false);
});
it("accepts IP literals", () => {
expect(isValidProxyHostDomain("192.168.1.10")).toBe(true);
expect(isValidProxyHostDomain("2001:db8::1")).toBe(true);
});
});
describe("normalizeProxyHostDomains", () => {
it("normalizes case, strips trailing dots, and deduplicates domains", () => {
expect(
normalizeProxyHostDomains([" *.Example.com. ", "APP.EXAMPLE.COM", "*.example.com"])
).toEqual(["*.example.com", "app.example.com"]);
});
it("throws a helpful error for invalid wildcard placement", () => {
expect(() => normalizeProxyHostDomains(["api.*.example.com"])).toThrow(
'Invalid domain "api.*.example.com". Wildcards are supported only as the left-most label, for example "*.example.com".'
);
});
});