Skip to content

Commit 70654b2

Browse files
pi0claude
andcommitted
test query: assert status, url params and req/res headers
Extend the RFC 10008 QUERY test to also verify the response is ok, that URL search params survive alongside the body, and that headers pass through both directions: the request Content-Type reaches the server and the Accept-Query / Content-Location response headers reach the client. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b69cfea commit 70654b2

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

server/handlers/query.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,58 @@ export default defineTestHandler(
1111
async (event) => {
1212
// A QUERY request carries content (like POST) but is safe & idempotent.
1313
const body = await event.req.json();
14-
return { method: event.req.method, echo: body };
14+
15+
// RFC 10008 §3 / §2.3: advertise QUERY support and point at a cacheable
16+
// result resource. Lets the client verify response headers pass through.
17+
event.res.headers.set("accept-query", "application/json");
18+
event.res.headers.set("content-location", event.url.pathname + event.url.search);
19+
20+
return {
21+
method: event.req.method,
22+
// RFC 10008 §2: Content-Type is mandatory and describes the body.
23+
contentType: event.req.headers.get("content-type"),
24+
// URL search params must survive alongside the body.
25+
params: Object.fromEntries(event.url.searchParams),
26+
echo: body,
27+
};
1528
},
1629
async ({ assert }) => {
17-
const res = await fetch("", {
30+
const res = await fetch("?table=users&limit=10", {
1831
method: "QUERY",
1932
headers: { "content-type": "application/json" },
2033
body: JSON.stringify({ sql: "select * from nitro" }),
21-
}).then((r) => r.json());
22-
assert(res.method === "QUERY", `Unexpected method: ${res.method}`);
23-
assert(res.echo?.sql === "select * from nitro", `Unexpected echo: ${JSON.stringify(res.echo)}`);
34+
});
35+
36+
assert(res.ok, `Unexpected status: ${res.status}`);
37+
38+
// Response headers round-trip to the client (RFC 10008 §2.3, §3).
39+
assert(
40+
res.headers.get("accept-query") === "application/json",
41+
`Unexpected Accept-Query: ${res.headers.get("accept-query")}`,
42+
);
43+
assert(
44+
(res.headers.get("content-location") || "").includes("table=users"),
45+
`Unexpected Content-Location: ${res.headers.get("content-location")}`,
46+
);
47+
48+
const data = await res.json();
49+
assert(data.method === "QUERY", `Unexpected method: ${data.method}`);
50+
51+
// Request header round-trip (server saw the mandatory Content-Type).
52+
assert(
53+
(data.contentType || "").includes("application/json"),
54+
`Unexpected request content-type: ${data.contentType}`,
55+
);
56+
57+
// URL search params round-trip alongside the body.
58+
assert(data.params.table === "users", `Unexpected table param: ${data.params.table}`);
59+
assert(data.params.limit === "10", `Unexpected limit param: ${data.params.limit}`);
60+
61+
// Body round-trip.
62+
assert(
63+
data.echo?.sql === "select * from nitro",
64+
`Unexpected echo: ${JSON.stringify(data.echo)}`,
65+
);
2466
},
2567
"server/handlers/query.ts",
2668
);

0 commit comments

Comments
 (0)