Cotton code
54 lines · 2.1 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import worker from "../index.ts";import type { Env } from "../env.ts";/** * The legacy-host guard runs before the asset binding is reached, so an Env * whose ASSETS answers a marker is enough to tell a redirect from a page. */const env = {  PUBLIC_ORIGIN: "https://cottondb.com",  ASSETS: { fetch: async () => new Response("page", { status: 200 }) },} as unknown as Env;async function answerFor(url: string) {  return worker.fetch(new Request(url), env);}describe("legacy host guard", () => {  it("308s cotton.tinmen.ai to cottondb.com, path and query intact", async () => {    const res = await answerFor("https://cotton.tinmen.ai/m/alex?src=status");    expect(res.status).toBe(308);    expect(res.headers.get("Location")).toBe("https://cottondb.com/m/alex?src=status");  });  it("collapses www onto the apex", async () => {    const res = await answerFor("https://www.cottondb.com/privacy");    expect(res.status).toBe(308);    expect(res.headers.get("Location")).toBe("https://cottondb.com/privacy");  });  it("leaves the canonical host to the asset layer", async () => {    const res = await answerFor("https://cottondb.com/");    expect(res.status).toBe(200);    expect(await res.text()).toBe("page");  });  it("sends the old internal page of test links to the home page's list of apps, which carries each app's links", async () => {    for (const u of ["https://cottondb.com/apps-5dc0fd79cb", "https://cottondb.com/apps-5dc0fd79cb.html"]) {      const res = await answerFor(u);      expect(res.status, u).toBe(302);      expect(res.headers.get("location"), u).toBe("https://cottondb.com/#get");    }  });  it("sends the first site's /about to the plain privacy page, /yours, which now holds its words", async () => {    for (const u of ["https://cottondb.com/about", "https://cottondb.com/about/"]) {      const res = await answerFor(u);      expect(res.status).toBe(301);      expect(res.headers.get("Location")).toBe("https://cottondb.com/yours");    }    expect((await answerFor("https://cottondb.com/aboutus")).status).toBe(200);  });});