g1t/services/pages/src/route.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { parseEntry, redirectTo, route } from "./route.ts"; |
| 5 | import worker from "./index.ts"; |
| 6 | |
| 7 | test("hosts on g1t.page are the home page, the fallback origin, or an app", () => { |
| 8 | assert.deepEqual(route("g1t.page"), { kind: "home" }); |
| 9 | assert.deepEqual(route("domains.g1t.page"), { kind: "fallback" }); |
| 10 | assert.deepEqual(route("web-acme.g1t.page"), { kind: "app", label: "web-acme" }); |
| 11 | assert.deepEqual(route("WEB-Acme.g1t.page."), { kind: "app", label: "web-acme" }); |
| 12 | assert.deepEqual(route("a.b.g1t.page"), { kind: "invalid" }); |
| 13 | }); |
| 14 | |
| 15 | test("any other host is a custom domain", () => { |
| 16 | assert.deepEqual(route("aut10dmain.io"), { kind: "custom", hostname: "aut10dmain.io" }); |
| 17 | assert.deepEqual(route("www.example.co.uk"), { kind: "custom", hostname: "www.example.co.uk" }); |
| 18 | assert.deepEqual(route("xn--bcher-kva.example"), { kind: "custom", hostname: "xn--bcher-kva.example" }); |
| 19 | assert.deepEqual(route("localhost"), { kind: "invalid" }); |
| 20 | assert.deepEqual(route("-bad.example"), { kind: "invalid" }); |
| 21 | }); |
| 22 | |
| 23 | test("entries are read only when they are well formed", () => { |
| 24 | assert.deepEqual(parseEntry({ script: "web-acme", redirect: null }), { script: "web-acme", redirect: null }); |
| 25 | assert.deepEqual(parseEntry({ script: "web-acme", redirect: "example.com" }), { script: "web-acme", redirect: "example.com" }); |
| 26 | assert.equal(parseEntry({ script: "../x" }), null); |
| 27 | assert.equal(parseEntry(null), null); |
| 28 | assert.equal(parseEntry("web-acme"), null); |
| 29 | }); |
| 30 | |
| 31 | test("redirects keep the path and query", () => { |
| 32 | assert.equal(redirectTo("https://www.example.com/a/b?x=1#h", "example.com"), "https://example.com/a/b?x=1"); |
| 33 | }); |
| 34 | |
| 35 | /** The worker with a namespace that knows `scripts`, and a KV holding `domains`. */ |
| 36 | function env(scripts: Record<string, string>, domains: Record<string, unknown>) { |
| 37 | const seen: string[] = []; |
| 38 | return { |
| 39 | seen, |
| 40 | env: { |
| 41 | APPS: { |
| 42 | get(name: string) { |
| 43 | return { |
| 44 | async fetch(request: Request) { |
| 45 | seen.push(`${name} ${new URL(request.url).hostname}`); |
| 46 | if (!(name in scripts)) throw new Error("Worker not found"); |
| 47 | return new Response(scripts[name]); |
| 48 | }, |
| 49 | }; |
| 50 | }, |
| 51 | }, |
| 52 | DOMAINS: { |
| 53 | async get(key: string) { |
| 54 | return domains[key] ?? null; |
| 55 | }, |
| 56 | }, |
| 57 | } as any, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | const call = (url: string, e: any) => worker.fetch(new Request(url), e, {} as any); |
| 62 | |
| 63 | test("a custom domain is served by its project's app, with the request's host kept", async () => { |
| 64 | const { env: e, seen } = env({ "web-acme": "hello" }, { "app.example.com": { script: "web-acme", redirect: null } }); |
| 65 | const response = await call("https://app.example.com/docs?q=1", e); |
| 66 | assert.equal(response.status, 200); |
| 67 | assert.equal(await response.text(), "hello"); |
| 68 | assert.deepEqual(seen, ["web-acme app.example.com"]); |
| 69 | }); |
| 70 | |
| 71 | test("a paired domain redirects with a 308, path and query kept", async () => { |
| 72 | const { env: e, seen } = env({}, { "www.example.org": { script: "web-acme", redirect: "example.org" } }); |
| 73 | const response = await call("https://www.example.org/pricing?plan=pro", e); |
| 74 | assert.equal(response.status, 308); |
| 75 | assert.equal(response.headers.get("location"), "https://example.org/pricing?plan=pro"); |
| 76 | assert.deepEqual(seen, []); |
| 77 | }); |
| 78 | |
| 79 | test("an unknown custom domain gets the branded 404", async () => { |
| 80 | const { env: e } = env({}, {}); |
| 81 | const response = await call("https://nobody.example.net/", e); |
| 82 | assert.equal(response.status, 404); |
| 83 | assert.match(await response.text(), /This domain is not set up/); |
| 84 | }); |
| 85 | |
| 86 | test("a custom domain whose app is not up says so", async () => { |
| 87 | const { env: e } = env({}, { "shop.example.com": { script: "shop-acme", redirect: null } }); |
| 88 | const response = await call("https://shop.example.com/", e); |
| 89 | assert.equal(response.status, 404); |
| 90 | assert.match(await response.text(), /Nothing deployed here yet/); |
| 91 | }); |
| 92 | |
| 93 | test("g1t.page addresses dispatch by label as before", async () => { |
| 94 | const { env: e, seen } = env({ "web-acme": "prod", "web-git-x-acme": "preview" }, {}); |
| 95 | assert.equal(await (await call("https://web-acme.g1t.page/", e)).text(), "prod"); |
| 96 | const preview = await call("https://web-git-x-acme.g1t.page/", e); |
| 97 | assert.equal(preview.headers.get("x-robots-tag"), "noindex"); |
| 98 | assert.deepEqual(seen, ["web-acme web-acme.g1t.page", "web-git-x-acme web-git-x-acme.g1t.page"]); |
| 99 | assert.equal((await call("https://gone-acme.g1t.page/", e)).status, 404); |
| 100 | assert.equal((await call("https://domains.g1t.page/", e)).status, 200); |
| 101 | }); |