|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import worker, { SECURITY_HEADERS, type Env } from "../../src/worker/index"; |
| 3 | +import { collectLogs, findLog, ALLOWED_ORIGIN } from "./helpers"; |
| 4 | + |
| 5 | +function makeEnv(overrides: Partial<Env> = {}): Env { |
| 6 | + return { |
| 7 | + ASSETS: { fetch: async () => new Response("asset") }, |
| 8 | + GITHUB_CLIENT_ID: "test_client_id", |
| 9 | + GITHUB_CLIENT_SECRET: "test_client_secret", |
| 10 | + ALLOWED_ORIGIN, |
| 11 | + SENTRY_DSN: "https://abc123@o123456.ingest.sentry.io/7890123", |
| 12 | + SESSION_KEY: "dGVzdC1zZXNzaW9uLWtleQ==", |
| 13 | + SEAL_KEY: "dGVzdC1zZWFsLWtleQ==", |
| 14 | + TURNSTILE_SECRET_KEY: "test-turnstile-secret", |
| 15 | + PROXY_RATE_LIMITER: { limit: vi.fn().mockResolvedValue({ success: true }) }, |
| 16 | + ...overrides, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe("Worker static asset handling for /_app/*", () => { |
| 21 | + let consoleSpy: { |
| 22 | + info: ReturnType<typeof vi.spyOn>; |
| 23 | + warn: ReturnType<typeof vi.spyOn>; |
| 24 | + error: ReturnType<typeof vi.spyOn>; |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + consoleSpy = { |
| 29 | + info: vi.spyOn(console, "info").mockImplementation(() => {}), |
| 30 | + warn: vi.spyOn(console, "warn").mockImplementation(() => {}), |
| 31 | + error: vi.spyOn(console, "error").mockImplementation(() => {}), |
| 32 | + }; |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.restoreAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns 404 with no-store, SECURITY_HEADERS, and logs stale_asset_404 on GET /_app/* when ASSETS returns 200 text/html", async () => { |
| 40 | + const env = makeEnv({ |
| 41 | + ASSETS: { |
| 42 | + fetch: async () => |
| 43 | + new Response("<!doctype html><html><body>SPA</body></html>", { |
| 44 | + status: 200, |
| 45 | + headers: { "Content-Type": "text/html" }, |
| 46 | + }), |
| 47 | + }, |
| 48 | + }); |
| 49 | + const req = new Request("https://gh.gordoncode.dev/_app/missing-abc123.js", { |
| 50 | + method: "GET", |
| 51 | + }); |
| 52 | + |
| 53 | + const resp = await worker.fetch(req, env); |
| 54 | + |
| 55 | + expect(resp.status).toBe(404); |
| 56 | + expect(await resp.text()).toBe("Not Found"); |
| 57 | + expect(resp.headers.get("Cache-Control")).toBe("no-store"); |
| 58 | + expect(resp.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); |
| 59 | + for (const [key, value] of Object.entries(SECURITY_HEADERS)) { |
| 60 | + expect(resp.headers.get(key)).toBe(value); |
| 61 | + } |
| 62 | + |
| 63 | + const logs = collectLogs(consoleSpy); |
| 64 | + const staleLog = findLog(logs, "stale_asset_404"); |
| 65 | + expect(staleLog).toBeDefined(); |
| 66 | + expect(staleLog?.level).toBe("warn"); |
| 67 | + expect(staleLog?.entry.pathname).toBe("/_app/missing-abc123.js"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns 404 on GET /_app/* when ASSETS returns 200 text/html; charset=utf-8", async () => { |
| 71 | + const env = makeEnv({ |
| 72 | + ASSETS: { |
| 73 | + fetch: async () => |
| 74 | + new Response("<!doctype html><html><body>SPA</body></html>", { |
| 75 | + status: 200, |
| 76 | + headers: { "Content-Type": "text/html; charset=utf-8" }, |
| 77 | + }), |
| 78 | + }, |
| 79 | + }); |
| 80 | + const req = new Request("https://gh.gordoncode.dev/_app/missing-abc123.js", { |
| 81 | + method: "GET", |
| 82 | + }); |
| 83 | + |
| 84 | + const resp = await worker.fetch(req, env); |
| 85 | + |
| 86 | + expect(resp.status).toBe(404); |
| 87 | + expect(await resp.text()).toBe("Not Found"); |
| 88 | + expect(resp.headers.get("Cache-Control")).toBe("no-store"); |
| 89 | + expect(resp.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); |
| 90 | + for (const [key, value] of Object.entries(SECURITY_HEADERS)) { |
| 91 | + expect(resp.headers.get(key)).toBe(value); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it("returns 404 with empty body on HEAD /_app/* when ASSETS returns 200 text/html", async () => { |
| 96 | + const env = makeEnv({ |
| 97 | + ASSETS: { |
| 98 | + fetch: async () => |
| 99 | + new Response("<!doctype html><html><body>SPA</body></html>", { |
| 100 | + status: 200, |
| 101 | + headers: { "Content-Type": "text/html" }, |
| 102 | + }), |
| 103 | + }, |
| 104 | + }); |
| 105 | + const req = new Request("https://gh.gordoncode.dev/_app/missing-abc123.js", { |
| 106 | + method: "HEAD", |
| 107 | + }); |
| 108 | + |
| 109 | + const resp = await worker.fetch(req, env); |
| 110 | + |
| 111 | + expect(resp.status).toBe(404); |
| 112 | + expect(resp.body).toBeNull(); |
| 113 | + expect(await resp.text()).toBe(""); |
| 114 | + expect(resp.headers.get("Cache-Control")).toBe("no-store"); |
| 115 | + expect(resp.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); |
| 116 | + for (const [key, value] of Object.entries(SECURITY_HEADERS)) { |
| 117 | + expect(resp.headers.get(key)).toBe(value); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it("passes through 200 application/javascript response for /_app/index-abc.js", async () => { |
| 122 | + const env = makeEnv({ |
| 123 | + ASSETS: { |
| 124 | + fetch: async () => |
| 125 | + new Response("console.log('ok');", { |
| 126 | + status: 200, |
| 127 | + headers: { |
| 128 | + "Content-Type": "application/javascript", |
| 129 | + "Cache-Control": "public, max-age=31536000, immutable", |
| 130 | + }, |
| 131 | + }), |
| 132 | + }, |
| 133 | + }); |
| 134 | + const req = new Request("https://gh.gordoncode.dev/_app/index-abc.js", { |
| 135 | + method: "GET", |
| 136 | + }); |
| 137 | + |
| 138 | + const resp = await worker.fetch(req, env); |
| 139 | + |
| 140 | + expect(resp.status).toBe(200); |
| 141 | + expect(await resp.text()).toBe("console.log('ok');"); |
| 142 | + expect(resp.headers.get("Content-Type")).toBe("application/javascript"); |
| 143 | + expect(resp.headers.get("Cache-Control")).toBe( |
| 144 | + "public, max-age=31536000, immutable" |
| 145 | + ); |
| 146 | + expect(findLog(collectLogs(consoleSpy), "stale_asset_404")).toBeUndefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + it("passes through 200 text/html SPA fallback for non-/_app/ routes like /dashboard", async () => { |
| 150 | + const html = "<!doctype html><html><body><div id='root'></div></body></html>"; |
| 151 | + const env = makeEnv({ |
| 152 | + ASSETS: { |
| 153 | + fetch: async () => |
| 154 | + new Response(html, { |
| 155 | + status: 200, |
| 156 | + headers: { "Content-Type": "text/html; charset=utf-8" }, |
| 157 | + }), |
| 158 | + }, |
| 159 | + }); |
| 160 | + const req = new Request("https://gh.gordoncode.dev/dashboard", { |
| 161 | + method: "GET", |
| 162 | + }); |
| 163 | + |
| 164 | + const resp = await worker.fetch(req, env); |
| 165 | + |
| 166 | + expect(resp.status).toBe(200); |
| 167 | + expect(await resp.text()).toBe(html); |
| 168 | + expect(resp.headers.get("Content-Type")).toBe("text/html; charset=utf-8"); |
| 169 | + expect(findLog(collectLogs(consoleSpy), "stale_asset_404")).toBeUndefined(); |
| 170 | + }); |
| 171 | + |
| 172 | + it("passes through 304 Not Modified response from ASSETS for /_app/x.js", async () => { |
| 173 | + const env = makeEnv({ |
| 174 | + ASSETS: { |
| 175 | + fetch: async () => |
| 176 | + new Response(null, { |
| 177 | + status: 304, |
| 178 | + headers: { ETag: '"abc123"' }, |
| 179 | + }), |
| 180 | + }, |
| 181 | + }); |
| 182 | + const req = new Request("https://gh.gordoncode.dev/_app/x.js", { |
| 183 | + method: "GET", |
| 184 | + }); |
| 185 | + |
| 186 | + const resp = await worker.fetch(req, env); |
| 187 | + |
| 188 | + expect(resp.status).toBe(304); |
| 189 | + expect(resp.headers.get("ETag")).toBe('"abc123"'); |
| 190 | + expect(findLog(collectLogs(consoleSpy), "stale_asset_404")).toBeUndefined(); |
| 191 | + }); |
| 192 | +}); |
0 commit comments