pr_01m47d15m3e54sn21z27rpy5n9/services/deployments/src/hostname.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { apexOf, checkHostname, isApex, recordName, twinOf } from "./hostname.ts"; |
| 5 | import { recordsOf, statusOf } from "./custom-hostnames.ts"; |
| 6 | |
| 7 | const host = (input: string) => { |
| 8 | const checked = checkHostname(input); |
| 9 | return checked.ok ? checked.hostname : null; |
| 10 | }; |
| 11 | |
| 12 | test("hostnames are lowercased and cleaned of what people paste", () => { |
| 13 | assert.equal(host("Aut10dmain.IO"), "aut10dmain.io"); |
| 14 | assert.equal(host(" https://www.example.com/some/path?x=1 "), "www.example.com"); |
| 15 | assert.equal(host("example.com."), "example.com"); |
| 16 | }); |
| 17 | |
| 18 | test("international names become punycode", () => { |
| 19 | assert.equal(host("bücher.example"), "xn--bcher-kva.example"); |
| 20 | }); |
| 21 | |
| 22 | test("what is not a hostname g1t can serve is refused", () => { |
| 23 | for (const bad of [ |
| 24 | "", |
| 25 | "localhost", |
| 26 | "*.example.com", |
| 27 | "example.com:8080", |
| 28 | "1.2.3.4", |
| 29 | "user@example.com", |
| 30 | "-bad.example.com", |
| 31 | "bad-.example.com", |
| 32 | `${"a".repeat(64)}.com`, |
| 33 | `${"a.".repeat(130)}com`, |
| 34 | "exa mple.com", |
| 35 | ]) { |
| 36 | assert.equal(checkHostname(bad).ok, false, bad); |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | test("g1t's own domains are refused", () => { |
| 41 | for (const own of ["g1t.page", "web-acme.g1t.page", "domains.g1t.page", "g1t.sh", "docs.g1t.sh"]) { |
| 42 | const checked = checkHostname(own); |
| 43 | assert.equal(checked.ok, false, own); |
| 44 | } |
| 45 | assert.equal(host("notg1t.page"), "notg1t.page"); |
| 46 | }); |
| 47 | |
| 48 | test("apex and www twins", () => { |
| 49 | assert.equal(isApex("example.com"), true); |
| 50 | assert.equal(isApex("www.example.com"), false); |
| 51 | assert.equal(isApex("example.co.uk"), true); |
| 52 | assert.equal(apexOf("app.shop.example.co.uk"), "example.co.uk"); |
| 53 | assert.equal(twinOf("example.com"), "www.example.com"); |
| 54 | assert.equal(twinOf("www.example.com"), "example.com"); |
| 55 | assert.equal(twinOf("app.example.com"), null); |
| 56 | assert.equal(twinOf("www.app.example.com"), null); |
| 57 | assert.equal(recordName("example.com"), "@"); |
| 58 | assert.equal(recordName("www.example.com"), "www"); |
| 59 | }); |
| 60 | |
| 61 | test("Cloudflare's states become a domain's", () => { |
| 62 | assert.equal(statusOf({ id: "1", hostname: "a.com", status: "pending", ssl: { status: "pending_validation" } }).status, "pending"); |
| 63 | assert.equal(statusOf({ id: "1", hostname: "a.com", status: "active", ssl: { status: "pending_validation" } }).status, "verifying"); |
| 64 | assert.equal(statusOf({ id: "1", hostname: "a.com", status: "active", ssl: { status: "active" } }).status, "active"); |
| 65 | assert.equal(statusOf({ id: "1", hostname: "a.com", status: "blocked" }).status, "failed"); |
| 66 | assert.equal(statusOf({ id: "1", hostname: "a.com", status: "pending", ssl: { status: "validation_timed_out" } }).status, "failed"); |
| 67 | }); |
| 68 | |
| 69 | test("the records Cloudflare asks for are listed", () => { |
| 70 | const records = recordsOf({ |
| 71 | id: "1", |
| 72 | hostname: "a.com", |
| 73 | status: "pending", |
| 74 | ownership_verification: { type: "txt", name: "_cf-custom-hostname.a.com", value: "abc" }, |
| 75 | ssl: { validation_records: [{ txt_name: "_acme-challenge.a.com", txt_value: "xyz" }, { http_url: "http://a.com/.well-known/x", http_body: "b" }] }, |
| 76 | }); |
| 77 | assert.deepEqual( |
| 78 | records.map((r) => `${r.type} ${r.name} ${r.value}`), |
| 79 | ["TXT _cf-custom-hostname.a.com abc", "TXT _acme-challenge.a.com xyz"], |
| 80 | ); |
| 81 | }); |