import assert from "node:assert/strict"; import test from "node:test"; import { validateLoopbackHttpUrl, validateTcpPort } from "../validate-loopback-url.mjs"; for (const [input, expected] of [ ["http://127.0.0.1:5178", "http://127.0.0.1:5178"], ["http://localhost:3000/", "http://localhost:3000"], ["http://[::1]:4173", "http://[::1]:4173"], ]) { test(`accepts loopback origin ${input}`, () => { assert.equal(validateLoopbackHttpUrl(input), expected); }); } for (const input of ["1", "80", "5178", "65535"]) { test(`accepts canonical TCP port ${input}`, () => { assert.equal(validateTcpPort(input), input); }); } for (const input of ["", "0", "065", "65536", "5178/path", "5178@remote.invalid", "1.5", "-1"]) { test(`rejects ambiguous or invalid TCP port ${input || ""}`, () => { assert.throws(() => validateTcpPort(input)); }); } for (const input of [ "https://127.0.0.1:5178", "http://example.invalid:5178", "http://localhost.example.invalid:5178", "http://localhost@remote.invalid:5178", "http://remote.invalid@localhost:5178", "http://user:pass@127.0.0.1:5178", "http://127.0.0.1:5178/path", "not-a-url", ]) { test(`rejects non-loopback or ambiguous target ${input}`, () => { assert.throws(() => validateLoopbackHttpUrl(input)); }); }