|
3 | 3 | fetchNodeRequestHandler,
|
4 | 4 | callNodeRequestHandler,
|
5 | 5 | type NodeRequestHandler,
|
| 6 | + type NodeRequestHeaders, |
6 | 7 | } from "../src";
|
7 | 8 |
|
8 | 9 | const echoHandler: NodeRequestHandler = (req, res) => {
|
@@ -31,41 +32,45 @@ describe("fetchNodeRequestHandler", () => {
|
31 | 32 | });
|
32 | 33 | });
|
33 | 34 |
|
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: [ |
57 | 53 | ["foo", "bar"],
|
58 | 54 | ["empty", ""],
|
59 | 55 | ["array", "a"],
|
60 | 56 | ["array", "b"],
|
| 57 | + ["array", ""], |
61 | 58 | ["array", "c"],
|
62 | 59 | ],
|
| 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 }); |
63 | 72 | });
|
64 |
| - expect(await res.json()).toEqual({ |
65 |
| - url: "/test", |
66 |
| - headers: { foo: "bar", host: "localhost", array: ["a", "b", "c"] }, |
67 |
| - }); |
68 |
| - }); |
| 73 | + } |
69 | 74 |
|
70 | 75 | it("error response", async () => {
|
71 | 76 | const res = await fetchNodeRequestHandler(() => {
|
|
0 commit comments