Something went wrong. Try again.
Automations and webhooks for the AT Protocol airglow.run
automation webhook atproto atprotocol
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import { describe, it, expect } from "vitest";import { Hono } from "hono";import { setPageCache } from "./http.js";
const ANON = "public, s-maxage=300, stale-while-revalidate=3600";
async function headers(viewer: unknown, opts?: { fragment?: boolean }) { const app = new Hono().get("/", (c) => { setPageCache(c, viewer, ANON, opts); return c.text("ok"); }); const res = await app.request("http://localhost/"); return { vary: res.headers.get("Vary"), cacheControl: res.headers.get("Cache-Control"), };}
describe("setPageCache", () => { it("lets a shared cache store the anonymous render, keyed on the cookie", async () => { expect(await headers(null)).toEqual({ vary: "Cookie", cacheControl: ANON }); });
it("keeps the signed-in render out of shared caches", async () => { expect(await headers({ handle: "alice.test" })).toEqual({ vary: "Cookie", cacheControl: "private, no-store", }); });
// The pair is the point: `private, no-store` alone would still let a stored // anonymous copy be served to a signed-in viewer, and `Vary: Cookie` alone // would still let a signed-in render be stored and handed to everyone. it("always sets Vary, whoever is viewing", async () => { expect((await headers(null)).vary).toBe("Cookie"); expect((await headers({ handle: "alice.test" })).vary).toBe("Cookie"); });
it("treats an absent viewer as anonymous however it is spelled", async () => { for (const viewer of [null, undefined, false, ""]) { expect((await headers(viewer)).cacheControl).toBe(ANON); } });
// A route that branches on `X-Fragment` returns two different bodies under // one URL. Without the header in `Vary`, a shared cache keyed on the URL // alone can store the bare fragment and serve it to a navigation, which // renders as a page with no chrome. describe("a route that branches on X-Fragment", () => { it("keys the anonymous render on the fragment header too", async () => { expect(await headers(null, { fragment: true })).toEqual({ vary: "Cookie, X-Fragment", cacheControl: ANON, }); });
it("still keys on Cookie, whoever is viewing", async () => { for (const viewer of [null, { handle: "alice.test" }]) { expect((await headers(viewer, { fragment: true })).vary).toBe("Cookie, X-Fragment"); } });
it("leaves a route that serves one body per URL keyed on Cookie alone", async () => { expect((await headers(null, { fragment: false })).vary).toBe("Cookie"); expect((await headers(null, {})).vary).toBe("Cookie"); expect((await headers(null)).vary).toBe("Cookie"); }); });});