Skip to content

Commit 943308b

Browse files
committed
tests for lambda url event
1 parent b7245c3 commit 943308b

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

test/eventToRequestOptions.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import eventHttpApiV2 from "./fixtures/event-http-api-2.0.json"
66
import eventHttpApiLegacy from "./fixtures/event-http-api.json"
77
import eventHealth from "./fixtures/health-event.json"
88
import eventAlb from "./fixtures/alb-event.json"
9+
import eventLambdaUrl from "./fixtures/event-lambda-url.json"
910
import evenMultiHeadertAlb from "./fixtures/alb-multi-header-event.json"
1011

1112
describe("eventToRequestOptions", () => {
@@ -119,6 +120,48 @@ describe("eventToRequestOptions", () => {
119120
})
120121
})
121122

123+
it("converts Lambda Url event to RequestOptions object", () => {
124+
const reqOpts = eventToRequestOptions(eventLambdaUrl)
125+
expect(reqOpts).toEqual({
126+
method: "GET",
127+
path: {
128+
pathname: "/inspect",
129+
protocol: "https",
130+
query: { param: "ab cd" },
131+
},
132+
remoteAddress: "9.9.9.9",
133+
payload: Buffer.alloc(0),
134+
headers: {
135+
accept:
136+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
137+
"accept-encoding": "gzip, deflate, br",
138+
"accept-language":
139+
"en-AU,en;q=0.9,pl-PL;q=0.8,pl;q=0.7,en-GB;q=0.6,en-US;q=0.5",
140+
"cache-control": "no-cache",
141+
cookie: "cookie1=value1",
142+
host: "uh7wqgxfah6pv7xm35xyjmev3e0tifwv.lambda-url.ap-southeast-2.on.aws",
143+
pragma: "no-cache",
144+
"sec-ch-ua":
145+
'"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
146+
"sec-ch-ua-mobile": "?0",
147+
"sec-ch-ua-platform": '"macOS"',
148+
"sec-fetch-dest": "document",
149+
"sec-fetch-mode": "navigate",
150+
"sec-fetch-site": "none",
151+
"sec-fetch-user": "?1",
152+
"upgrade-insecure-requests": "1",
153+
"user-agent":
154+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
155+
"x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
156+
"x-amzn-tls-version": "TLSv1.2",
157+
"x-amzn-trace-id": "Root=1-63833daf-2b68425f0431c7477a997202",
158+
"x-forwarded-for": "9.9.9.9",
159+
"x-forwarded-port": "443",
160+
"x-forwarded-proto": "https",
161+
},
162+
})
163+
})
164+
122165
it("converts Api Gateway HTTP legacy event to RequestOptions object", () => {
123166
const reqOpts = eventToRequestOptions(eventHttpApiLegacy)
124167
expect(reqOpts).toEqual({

test/fixtures/event-lambda-url.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/inspect",
5+
"rawQueryString": "param=ab%20cd",
6+
"cookies": [
7+
"cookie1=value1"
8+
],
9+
"headers": {
10+
"sec-fetch-mode": "navigate",
11+
"x-amzn-tls-version": "TLSv1.2",
12+
"sec-fetch-site": "none",
13+
"accept-language": "en-AU,en;q=0.9,pl-PL;q=0.8,pl;q=0.7,en-GB;q=0.6,en-US;q=0.5",
14+
"x-forwarded-proto": "https",
15+
"x-forwarded-port": "443",
16+
"x-forwarded-for": "9.9.9.9",
17+
"sec-fetch-user": "?1",
18+
"pragma": "no-cache",
19+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
20+
"x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
21+
"sec-ch-ua": "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"",
22+
"sec-ch-ua-mobile": "?0",
23+
"x-amzn-trace-id": "Root=1-63833daf-2b68425f0431c7477a997202",
24+
"sec-ch-ua-platform": "\"macOS\"",
25+
"host": "uh7wqgxfah6pv7xm35xyjmev3e0tifwv.lambda-url.ap-southeast-2.on.aws",
26+
"upgrade-insecure-requests": "1",
27+
"cache-control": "no-cache",
28+
"accept-encoding": "gzip, deflate, br",
29+
"sec-fetch-dest": "document",
30+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
31+
},
32+
"queryStringParameters": {
33+
"param": "ab cd"
34+
},
35+
"requestContext": {
36+
"accountId": "anonymous",
37+
"apiId": "uh7wqgxfah6pv7xm35xyjmev3e0tifwv",
38+
"domainName": "uh7wqgxfah6pv7xm35xyjmev3e0tifwv.lambda-url.ap-southeast-2.on.aws",
39+
"domainPrefix": "uh7wqgxfah6pv7xm35xyjmev3e0tifwv",
40+
"http": {
41+
"method": "GET",
42+
"path": "/inspect",
43+
"protocol": "HTTP/1.1",
44+
"sourceIp": "9.9.9.9",
45+
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
46+
},
47+
"requestId": "b56a2b87-d96e-4d64-9092-5dd4097011cb",
48+
"routeKey": "$default",
49+
"stage": "$default",
50+
"time": "27/Nov/2022:10:36:31 +0000",
51+
"timeEpoch": 1669545391654
52+
},
53+
"isBase64Encoded": false
54+
}

test/integration.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import eventRestApi from "./fixtures/event-rest-api.json"
66
import eventHttpApi from "./fixtures/event-http-api.json"
77
import eventHttpApiV1 from "./fixtures/event-http-api-1.0.json"
88
import eventHttpApiV2 from "./fixtures/event-http-api-2.0.json"
9+
import eventLambdaUrl from "./fixtures/event-lambda-url.json"
910
import eventAlb from "./fixtures/alb-event.json"
1011

1112
const handler = lambda(app)
@@ -271,6 +272,49 @@ describe("integration", () => {
271272
})
272273
})
273274

275+
it("handles Lambda Url event", async () => {
276+
const response = await handler(eventLambdaUrl)
277+
expect(response.statusCode).toEqual(200)
278+
expect(response.isBase64Encoded).toEqual(false)
279+
expect(response.headers!["content-type"]).toEqual(
280+
"application/json; charset=utf-8"
281+
)
282+
expect(response.headers!["x-powered-by"]).toEqual("Express")
283+
expect(response.multiValueHeaders).toBeUndefined()
284+
const json = JSON.parse(response.body)
285+
expect(json).toEqual({
286+
baseUrl: "",
287+
body: {},
288+
cookies: {
289+
cookie1: "value1",
290+
},
291+
fresh: false,
292+
hostname:
293+
"uh7wqgxfah6pv7xm35xyjmev3e0tifwv.lambda-url.ap-southeast-2.on.aws",
294+
ip: "9.9.9.9",
295+
ips: ["9.9.9.9"],
296+
method: "GET",
297+
originalUrl: "/inspect?param=ab+cd",
298+
params: {},
299+
path: "/inspect",
300+
protocol: "https",
301+
query: {
302+
param: "ab cd",
303+
},
304+
secure: true,
305+
signedCookies: {},
306+
stale: true,
307+
subdomains: [
308+
"ap-southeast-2",
309+
"lambda-url",
310+
"uh7wqgxfah6pv7xm35xyjmev3e0tifwv",
311+
],
312+
url: "/inspect?param=ab+cd",
313+
xForwardedFor: "9.9.9.9",
314+
xhr: false,
315+
})
316+
})
317+
274318
it("handles ALB event", async () => {
275319
const response = await handler(eventAlb)
276320
expect(response.statusCode).toEqual(200)

0 commit comments

Comments
 (0)