// The HTTP check refuses what is not http(s) and what would change something, and reports a status, // a refusal and a timeout as results (novox/hq ADR 0188: a tools bundle is read-only and harmless). import { test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; import { checkHttp, checkedMethod, checkedUrl } from "../http.ts"; test("only http and https URLs are checked", () => { for (const bad of ["file:///etc/passwd", "ftp://example.org/", "data:text/plain,hi", "javascript:1", "", "not a url"]) { assert.throws(() => checkedUrl(bad), `${bad} was accepted`); } assert.equal(checkedUrl("https://example.org/x").protocol, "https:"); }); test("only HEAD and GET are used", () => { assert.equal(checkedMethod(undefined), "HEAD"); assert.equal(checkedMethod("get"), "GET"); for (const bad of ["POST", "PUT", "DELETE", "PATCH"]) assert.throws(() => checkedMethod(bad)); }); test("a status, its headers and a redirect not followed", async () => { const server = createServer((req, res) => { if (req.url === "/moved") { res.writeHead(302, { location: "/elsewhere" }); res.end(); return; } res.writeHead(200, { "content-type": "text/plain", "x-secret": "not reported" }); res.end(req.method === "GET" ? "body" : undefined); }); await new Promise((ok) => server.listen(0, "127.0.0.1", ok)); const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; try { const head = await checkHttp({ url: base + "/" }); assert.equal(head.status, 200); assert.equal(head.method, "HEAD"); assert.equal(head.headers["content-type"], "text/plain"); assert.equal(head.headers["x-secret"], undefined); const moved = await checkHttp({ url: base + "/moved", method: "GET" }); assert.equal(moved.status, 302); assert.equal(moved.headers.location, "/elsewhere"); } finally { server.close(); } }); test("a refused connection is a result with its error", async () => { const server = createServer(); await new Promise((ok) => server.listen(0, "127.0.0.1", ok)); const port = (server.address() as AddressInfo).port; await new Promise((ok) => server.close(() => ok())); const got = await checkHttp({ url: `http://127.0.0.1:${port}/`, timeout_ms: 2000 }); assert.equal(got.status, undefined); assert.ok(got.error, "no error reported"); });