Skip to content

Commit 5486686

Browse files
authored
Add tests for converters (#556)
1 parent 7bcb7b7 commit 5486686

File tree

7 files changed

+1053
-132
lines changed

7 files changed

+1053
-132
lines changed

packages/open-next/src/converters/aws-apigw-v2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async function convertToApiGatewayProxyResultV2(
9191
): Promise<APIGatewayProxyResultV2> {
9292
const headers: Record<string, string> = {};
9393
Object.entries(result.headers)
94+
.map(([key, value]) => [key.toLowerCase(), value] as const)
9495
.filter(
9596
([key]) =>
9697
!CloudFrontBlacklistedHeaders.some((header) =>

packages/open-next/src/http/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export class IncomingMessage extends http.IncomingMessage {
1515
}: {
1616
method: string;
1717
url: string;
18-
headers: Record<string, string>;
18+
headers: Record<string, string | string[]>;
1919
body?: Buffer;
20-
remoteAddress: string;
20+
remoteAddress?: string;
2121
}) {
2222
super({
2323
encrypted: true,
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/* eslint-disable sonarjs/no-duplicate-string */
2+
import converter from "@opennextjs/aws/converters/aws-apigw-v1.js";
3+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
4+
import { Readable } from "stream";
5+
6+
describe("convertTo", () => {
7+
describe("AWS API Gateway v2 Result", () => {
8+
it("Should parse the headers", async () => {
9+
const response = (await converter.convertTo({
10+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
11+
headers: {
12+
"content-type": "application/json",
13+
test: "test",
14+
},
15+
isBase64Encoded: false,
16+
statusCode: 200,
17+
})) as APIGatewayProxyResult;
18+
19+
expect(response.headers).toStrictEqual({
20+
"content-type": "application/json",
21+
test: "test",
22+
});
23+
});
24+
25+
it("Should parse the headers with arrays", async () => {
26+
const response = (await converter.convertTo({
27+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
28+
headers: {
29+
test: ["test1", "test2"],
30+
},
31+
isBase64Encoded: false,
32+
statusCode: 200,
33+
})) as APIGatewayProxyResult;
34+
35+
expect(response.multiValueHeaders).toStrictEqual({
36+
test: ["test1", "test2"],
37+
});
38+
});
39+
40+
it("Should parse single and array headers", async () => {
41+
const response = (await converter.convertTo({
42+
body: Readable.toWeb(Readable.from(Buffer.from(""))),
43+
headers: {
44+
single: "test",
45+
multi: ["test1", "test2"],
46+
},
47+
isBase64Encoded: false,
48+
statusCode: 200,
49+
})) as APIGatewayProxyResult;
50+
51+
expect(response.headers).toStrictEqual({
52+
single: "test",
53+
});
54+
expect(response.multiValueHeaders).toStrictEqual({
55+
multi: ["test1", "test2"],
56+
});
57+
});
58+
});
59+
});
60+
61+
describe("convertFrom", () => {
62+
it("Should convert a simple APIGatewayProxyEvent", async () => {
63+
const event: APIGatewayProxyEvent = {
64+
body: JSON.stringify({ message: "Hello, world!" }),
65+
headers: {
66+
"content-type": "application/json",
67+
},
68+
multiValueHeaders: {},
69+
httpMethod: "POST",
70+
isBase64Encoded: false,
71+
path: "/",
72+
pathParameters: null,
73+
queryStringParameters: null,
74+
multiValueQueryStringParameters: null,
75+
stageVariables: null,
76+
requestContext: {
77+
identity: {
78+
sourceIp: "::1",
79+
},
80+
} as any,
81+
resource: "",
82+
};
83+
84+
const response = await converter.convertFrom(event);
85+
86+
expect(response).toEqual({
87+
type: "core",
88+
method: "POST",
89+
rawPath: "/",
90+
url: "/",
91+
body: Buffer.from('{"message":"Hello, world!"}'),
92+
headers: {
93+
"content-type": "application/json",
94+
},
95+
remoteAddress: "::1",
96+
query: {},
97+
cookies: {},
98+
});
99+
});
100+
101+
it("Should handle multiValueHeaders", async () => {
102+
const event: APIGatewayProxyEvent = {
103+
body: JSON.stringify({ message: "Hello, world!" }),
104+
headers: {},
105+
multiValueHeaders: {
106+
test: ["test1", "test2"],
107+
},
108+
httpMethod: "POST",
109+
isBase64Encoded: false,
110+
path: "/",
111+
pathParameters: null,
112+
queryStringParameters: null,
113+
multiValueQueryStringParameters: null,
114+
stageVariables: null,
115+
requestContext: {
116+
identity: {
117+
sourceIp: "::1",
118+
},
119+
} as any,
120+
resource: "",
121+
};
122+
123+
const response = await converter.convertFrom(event);
124+
125+
expect(response).toEqual({
126+
type: "core",
127+
method: "POST",
128+
rawPath: "/",
129+
url: "/",
130+
body: Buffer.from('{"message":"Hello, world!"}'),
131+
headers: {
132+
test: "test1,test2",
133+
},
134+
remoteAddress: "::1",
135+
query: {},
136+
cookies: {},
137+
});
138+
});
139+
140+
it("Should handle queryStringParameters and multiValueQueryStringParameters", async () => {
141+
const event: APIGatewayProxyEvent = {
142+
body: JSON.stringify({ message: "Hello, world!" }),
143+
headers: {},
144+
multiValueHeaders: {},
145+
httpMethod: "POST",
146+
isBase64Encoded: false,
147+
path: "/",
148+
pathParameters: null,
149+
queryStringParameters: {
150+
test: "test",
151+
},
152+
multiValueQueryStringParameters: {
153+
test: ["test"],
154+
},
155+
stageVariables: null,
156+
requestContext: {
157+
identity: {
158+
sourceIp: "::1",
159+
},
160+
} as any,
161+
resource: "",
162+
};
163+
164+
const response = await converter.convertFrom(event);
165+
166+
expect(response).toEqual({
167+
type: "core",
168+
method: "POST",
169+
rawPath: "/",
170+
url: "/?test=test",
171+
body: Buffer.from('{"message":"Hello, world!"}'),
172+
headers: {},
173+
remoteAddress: "::1",
174+
query: {
175+
test: ["test"],
176+
},
177+
cookies: {},
178+
});
179+
});
180+
181+
it("Should handle cookies", async () => {
182+
const event: APIGatewayProxyEvent = {
183+
body: JSON.stringify({ message: "Hello, world!" }),
184+
headers: {
185+
"content-type": "application/json",
186+
},
187+
multiValueHeaders: {
188+
cookie: ["test1=1", "test2=2"],
189+
},
190+
httpMethod: "POST",
191+
isBase64Encoded: false,
192+
path: "/",
193+
pathParameters: null,
194+
queryStringParameters: null,
195+
multiValueQueryStringParameters: null,
196+
stageVariables: null,
197+
requestContext: {
198+
identity: {
199+
sourceIp: "::1",
200+
},
201+
} as any,
202+
resource: "",
203+
};
204+
205+
const response = await converter.convertFrom(event);
206+
207+
expect(response).toEqual({
208+
type: "core",
209+
method: "POST",
210+
rawPath: "/",
211+
url: "/",
212+
body: Buffer.from('{"message":"Hello, world!"}'),
213+
headers: {
214+
"content-type": "application/json",
215+
cookie: "test1=1,test2=2",
216+
},
217+
remoteAddress: "::1",
218+
query: {},
219+
cookies: {
220+
test1: "1",
221+
test2: "2",
222+
},
223+
});
224+
});
225+
226+
it("Should handle base64 encoded body", async () => {
227+
const event: APIGatewayProxyEvent = {
228+
body: Buffer.from("Hello, world!").toString("base64"),
229+
headers: {
230+
"content-type": "application/json",
231+
},
232+
multiValueHeaders: {},
233+
httpMethod: "GET",
234+
isBase64Encoded: true,
235+
path: "/",
236+
pathParameters: null,
237+
queryStringParameters: null,
238+
multiValueQueryStringParameters: null,
239+
stageVariables: null,
240+
requestContext: {
241+
identity: {
242+
sourceIp: "::1",
243+
},
244+
} as any,
245+
resource: "",
246+
};
247+
248+
const response = await converter.convertFrom(event);
249+
250+
expect(response).toEqual({
251+
type: "core",
252+
method: "GET",
253+
rawPath: "/",
254+
url: "/",
255+
body: Buffer.from("Hello, world!"),
256+
headers: {
257+
"content-type": "application/json",
258+
},
259+
remoteAddress: "::1",
260+
query: {},
261+
cookies: {},
262+
});
263+
});
264+
});

0 commit comments

Comments
 (0)