From 02cab85d6e63dc6fb1b06e3f4f7895c6e401cfff Mon Sep 17 00:00:00 2001 From: Aaron Filson Date: Mon, 30 Nov 2026 10:20:49 -0800 Subject: [PATCH] Support HEAD requests on catalog and piece read routes --- backend/src/handler.mjs | 14 ++++++++------ backend/test/shop-pages.test.mjs | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/backend/src/handler.mjs b/backend/src/handler.mjs index 2b789f8..f6e9bb7 100644 --- a/backend/src/handler.mjs +++ b/backend/src/handler.mjs @@ -34,17 +34,19 @@ export async function handler(event) { const method = event.requestContext?.http?.method ?? event.httpMethod; const path = event.rawPath ?? event.path; const route = path?.startsWith("/api/") ? path.slice(5) : ""; + const isRead = method === "GET" || method === "HEAD"; + const readReply = (res) => (method === "HEAD" ? { ...res, body: "" } : res); try { if (method === "OPTIONS") return respond(204, ""); if (route && !routes.has(route)) return respond(404, { errors: ["Not found"] }); - if (method === "GET" && path === "/api/catalog") - return respond(200, { items: await loadCatalog() }, { "cache-control": "public, max-age=15" }); + if (isRead && path === "/api/catalog") + return readReply(respond(200, { items: await loadCatalog() }, { "cache-control": "public, max-age=15" })); // Square ids are plain letters and digits: the path is passed on as sent (piece() checks it), never // decoded, since a malformed escape like /p/%E0 would throw and be answered as an unhandled error. - if (method === "GET" && routes.has("piece") && path === "/p/sitemap.xml") return await sitemap(); - if (method === "GET" && routes.has("piece") && path === "/p/feed.xml") return await feed(); - if (method === "GET" && routes.has("piece") && path === "/p/merchant.xml") return await merchantFeed(); - if (method === "GET" && routes.has("piece") && path?.startsWith("/p/")) return await piece(path.slice(3)); + if (isRead && routes.has("piece") && path === "/p/sitemap.xml") return readReply(await sitemap()); + if (isRead && routes.has("piece") && path === "/p/feed.xml") return readReply(await feed()); + if (isRead && routes.has("piece") && path === "/p/merchant.xml") return readReply(await merchantFeed()); + if (isRead && routes.has("piece") && path?.startsWith("/p/")) return readReply(await piece(path.slice(3))); if (method === "POST" && posts[path]) { const body = readBody(event); if (!body) return respond(400, { errors: ["Bad request."] }); diff --git a/backend/test/shop-pages.test.mjs b/backend/test/shop-pages.test.mjs index f75351c..0b97516 100644 --- a/backend/test/shop-pages.test.mjs +++ b/backend/test/shop-pages.test.mjs @@ -458,3 +458,20 @@ test("the pieces' sitemap and Atom feed list pieces with dates and photos", asyn })(); assert.ok(!soldMerchant.body.includes("I1"), "permanently sold one-of-a-kind piece omitted from merchant.xml"); }); + +test("HEAD requests on read routes mirror GET status and headers with an empty body", async () => { + _setSquare(fakeCatalog()); + const head = (rawPath) => handler({ requestContext: { http: { method: "HEAD" } }, rawPath }); + for (const path of ["/api/catalog", "/p/sitemap.xml", "/p/feed.xml", "/p/merchant.xml", "/p/I1"]) { + const g = await get(path); + const h = await head(path); + assert.equal(h.statusCode, 200, path); + assert.equal(h.headers["content-type"], g.headers["content-type"], path); + assert.equal(h.headers["cache-control"], g.headers["cache-control"], path); + assert.equal(h.body, "", path); + } + const gone = await head("/p/NOPE"); + assert.equal(gone.statusCode, 410); + assert.equal(gone.body, ""); + assert.equal((await head("/api/checkout")).statusCode, 404); +});