// A fake web for the generation tests (release 27 D7): a DuckDuckGo-shaped // HTML search page and a few pages, on loopback. No test reaches the real web. // // const web = await startFakeWeb({ results: { oven: [{ title, url, snippet }] } }); // web.searchUrl POST q=… here (the endpoint webSearch takes) // web.url("/page/a") a page on the fake // web.requests every request: { method, path, body, at } // await web.close() // // Routes: // POST /html/ results for the form's `q` (opts.results[q], else // the `*` entry, else none), each link wrapped in // DuckDuckGo's /l/?uddg= redirect, plus one ad // GET /page/ opts.pages[name] as text/html (a default page) // GET /text a plain-text page // GET /long a page longer than any default cut // GET /redirect?to= 302 to // GET /loop 302 to itself // GET /pdf application/pdf // GET /busy 429 (Retry-After: 1) for the first opts.busyTimes // requests (default 1), then a page // GET /down 503 always import http from "node:http"; const esc = (s) => String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); export function duckDuckGoHtml(results) { const items = results .map( (r) => ``, ) .join("\n"); const ad = ``; return `results${ad}\n${items}`; } const DEFAULT_PAGE = `The bakery & the oven

The bakery

The second shop opened in the spring.

It is a small place with a big oven.

  • bread
  • pies
footer text
`; export async function startFakeWeb(opts = {}) { const requests = []; let busy = opts.busyTimes ?? 1; const server = http.createServer(async (req, res) => { const chunks = []; for await (const c of req) chunks.push(c); const body = Buffer.concat(chunks).toString("utf8"); const u = new URL(req.url ?? "/", "http://fake"); requests.push({ method: req.method, path: u.pathname + u.search, body, at: Date.now(), ua: req.headers["user-agent"] }); const send = (status, type, text, headers = {}) => { res.writeHead(status, { "content-type": type, ...headers }); res.end(text); }; if (u.pathname === "/html/" && req.method === "POST") { const q = new URLSearchParams(body).get("q") ?? ""; const results = (opts.results ?? {})[q] ?? (opts.results ?? {})["*"] ?? []; return send(200, "text/html; charset=utf-8", duckDuckGoHtml(results)); } if (u.pathname.startsWith("/page/")) { const name = decodeURIComponent(u.pathname.slice(6)); return send(200, "text/html; charset=utf-8", (opts.pages ?? {})[name] ?? DEFAULT_PAGE); } if (u.pathname === "/text") return send(200, "text/plain", "plain words\r\nsecond line\n"); if (u.pathname === "/long") return send(200, "text/html", `

${"word ".repeat(30000)}

`); if (u.pathname === "/redirect") return send(302, "text/plain", "", { location: u.searchParams.get("to") ?? "/page/a" }); if (u.pathname === "/loop") return send(302, "text/plain", "", { location: "/loop" }); if (u.pathname === "/pdf") return send(200, "application/pdf", "%PDF-1.4"); if (u.pathname === "/busy") { if (busy > 0) { busy--; return send(429, "text/plain", "slow down", { "retry-after": "1" }); } return send(200, "text/html", DEFAULT_PAGE); } if (u.pathname === "/down") return send(503, "text/plain", "down"); return send(404, "text/plain", "not found"); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const port = server.address().port; const base = `http://127.0.0.1:${port}`; return { base, searchUrl: `${base}/html/`, url: (p) => `${base}${p}`, requests, close: () => new Promise((r) => server.close(() => r())), }; }