Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions backend/src/handler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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."] });
Expand Down
17 changes: 17 additions & 0 deletions backend/test/shop-pages.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,20 @@ test("the pieces' sitemap and Atom feed list pieces with dates and photos", asyn
})();
assert.ok(!soldMerchant.body.includes("<g:id>I1</g:id>"), "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);
});
Loading