Skip to content

Commit b69cfea

Browse files
committed
add query support test
1 parent e06d8ce commit b69cfea

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

nitro.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ export default defineConfig({
66
features: {
77
websocket: true,
88
},
9+
handlers: [
10+
// RFC 10008 (HTTP QUERY method): https://datatracker.ietf.org/doc/rfc10008/
11+
// File-based routing has no `.query` method suffix, so the handler is
12+
// registered manually here with an explicit method. GET serves the test
13+
// page; QUERY runs the actual RFC 10008 request.
14+
{ route: "/tests/query", method: "GET", handler: "./server/handlers/query.ts" },
15+
{ route: "/tests/query", method: "QUERY" as any, handler: "./server/handlers/query.ts" },
16+
],
917
publicAssets: [{ baseURL: "/_dist", dir: "./public/_dist", maxAge: 60 * 60 * 24 * 365 }],
1018
});

server/handlers/query.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { defineTestHandler } from "../utils/test";
2+
3+
// RFC 10008 — The HTTP QUERY method: https://datatracker.ietf.org/doc/rfc10008/
4+
//
5+
// File-based routing only understands the standard method suffixes
6+
// (`.get`, `.post`, ...), so there is no `.query.ts`. This handler therefore
7+
// lives outside the scanned `routes/` dir and is registered manually with an
8+
// explicit `method: "QUERY"` in `nitro.config.ts`.
9+
export default defineTestHandler(
10+
"query",
11+
async (event) => {
12+
// A QUERY request carries content (like POST) but is safe & idempotent.
13+
const body = await event.req.json();
14+
return { method: event.req.method, echo: body };
15+
},
16+
async ({ assert }) => {
17+
const res = await fetch("", {
18+
method: "QUERY",
19+
headers: { "content-type": "application/json" },
20+
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)}`);
24+
},
25+
"server/handlers/query.ts",
26+
);

server/routes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const tests = [
1010
"api",
1111
"form-data",
1212
"multipart-form-data",
13+
"query",
1314
// "sourcemap",
1415
"websocket",
1516
];

server/utils/test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export function defineTestHandler(
88
log: (text: string) => void;
99
assert: (condition: boolean, message: string) => void;
1010
}) => any,
11+
// Repo-relative path to the test source (defaults to the scanned `routes/tests` location).
12+
// Handlers registered manually via `nitro.config` live elsewhere and can override this.
13+
sourcePath: string = `server/routes/tests/${name}.ts`,
1114
) {
1215
return defineEventHandler(async (event) => {
1316
// Client
@@ -16,7 +19,7 @@ export function defineTestHandler(
1619
<pre id="logs"></pre>
1720
<hr />
1821
<a
19-
href="https://github.com/nitrojs/nitro-deploys/blob/main/server/routes/tests/${name}.ts"
22+
href="https://github.com/nitrojs/nitro-deploys/blob/main/${sourcePath}"
2023
target="_blank"
2124
>view source</a
2225
>

0 commit comments

Comments
 (0)