Skip to content

Commit 3f1640a

Browse files
authored
tests: refactor header tests (#4)
1 parent 9ebe124 commit 3f1640a

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

test/index.test.ts

+33-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
fetchNodeRequestHandler,
44
callNodeRequestHandler,
55
type NodeRequestHandler,
6+
type NodeRequestHeaders,
67
} from "../src";
78

89
const echoHandler: NodeRequestHandler = (req, res) => {
@@ -31,41 +32,45 @@ describe("fetchNodeRequestHandler", () => {
3132
});
3233
});
3334

34-
it("headers (object)", async () => {
35-
const res = await fetchNodeRequestHandler(echoHandler, "/test", {
36-
headers: { foo: "bar", empty: "" },
37-
});
38-
expect(await res.json()).toEqual({
39-
url: "/test",
40-
headers: { foo: "bar", host: "localhost" },
41-
});
42-
});
43-
44-
it("headers (Headers)", async () => {
45-
const res = await fetchNodeRequestHandler(echoHandler, "/test", {
46-
headers: new Headers({ foo: "bar", empty: "" }),
47-
});
48-
expect(await res.json()).toEqual({
49-
url: "/test",
50-
headers: { foo: "bar", host: "localhost" },
51-
});
52-
});
53-
54-
it("headers (array)", async () => {
55-
const res = await fetchNodeRequestHandler(echoHandler, "/test", {
56-
headers: [
35+
const requestHeaderTestCases: {
36+
description: string;
37+
input: HeadersInit & (HeadersInit | NodeRequestHeaders);
38+
expected: Record<string, string | string[]>;
39+
}[] = [
40+
{
41+
description: "Headers",
42+
input: new Headers({ foo: "bar", empty: "" }),
43+
expected: { foo: "bar", host: "localhost" },
44+
},
45+
{
46+
description: "object",
47+
input: { foo: "bar", empty: "" },
48+
expected: { foo: "bar", host: "localhost" },
49+
},
50+
{
51+
description: "array",
52+
input: [
5753
["foo", "bar"],
5854
["empty", ""],
5955
["array", "a"],
6056
["array", "b"],
57+
["array", ""],
6158
["array", "c"],
6259
],
60+
expected: { foo: "bar", host: "localhost", array: ["a", "b", "c"] },
61+
},
62+
];
63+
64+
for (const testCase of requestHeaderTestCases) {
65+
const { description, input, expected } = testCase;
66+
67+
it(`with request headers formatted as ${description}`, async () => {
68+
const res = await fetchNodeRequestHandler(echoHandler, "/test", {
69+
headers: input,
70+
});
71+
expect(await res.json()).toEqual({ url: "/test", headers: expected });
6372
});
64-
expect(await res.json()).toEqual({
65-
url: "/test",
66-
headers: { foo: "bar", host: "localhost", array: ["a", "b", "c"] },
67-
});
68-
});
73+
}
6974

7075
it("error response", async () => {
7176
const res = await fetchNodeRequestHandler(() => {

0 commit comments

Comments
 (0)