Skip to content

Commit

Permalink
test: add additional tests for req body handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 5, 2024
1 parent 6885842 commit e00b4c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 7 additions & 1 deletion test/_fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const server = serve({
},
})) satisfies ServerPlugin,
),
fetch(req) {
async fetch(req) {
const Response =
(globalThis as any).TEST_RESPONSE_CTOR || globalThis.Response;

Expand All @@ -39,6 +39,12 @@ export const server = serve({
case "/": {
return new Response("ok");
}
case "/body/binary": {
return new Response(req.body);
}
case "/body/text": {
return new Response(await req.text());
}
case "/ip": {
return new Response(`ip: ${req.xRemoteAddress}`);
}
Expand Down
24 changes: 22 additions & 2 deletions test/_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@ import { expect, test } from "vitest";

export function addTests(
url: (path: string) => string,
_opts?: { runtime?: string },
opts?: { runtime?: string },
) {
test("works", async () => {
test("GET works", async () => {
const response = await fetch(url("/"));
expect(response.status).toBe(200);
expect(await response.text()).toMatch("ok");
});

test("POST works (binary body)", async () => {
const response = await fetch(url("/body/binary"), {
method: "POST",
body: new Uint8Array([1, 2, 3]),
});
expect(response.status).toBe(200);
expect(new Uint8Array(await response.arrayBuffer())).toEqual(
new Uint8Array([1, 2, 3]),
);
});

test.skipIf(opts?.runtime === "node")("POST works (text body)", async () => {
const response = await fetch(url("/body/text"), {
method: "POST",
body: "hello world",
});
expect(response.status).toBe(200);
expect(await response.text()).toBe("hello world");
});

test("xRemoteAddress", async () => {
const response = await fetch(url("/ip"));
expect(response.status).toBe(200);
Expand Down

0 comments on commit e00b4c9

Please sign in to comment.