Skip to content

Commit 5dfbfa1

Browse files
committed
More integration tests and fixing cookies in v2.0 request
1 parent 3bf1fa3 commit 5dfbfa1

10 files changed

+123
-45
lines changed

src/eventToRequestOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const eventToRequestOptions = (event: APIGatewayEvent, ctx?: LambdaContext): InP
2222
let ssl = false;
2323
const queryStringParams = getValuesFromStringAndMultiString(event.queryStringParameters, event.multiValueQueryStringParameters, false);
2424
const headers = getValuesFromStringAndMultiString(event.headers, event.multiValueHeaders);
25+
if (Array.isArray(event.cookies)) {
26+
headers['cookie'] = event.cookies.join('; ')
27+
}
2528
if (ctx) {
2629
headers['x-aws-lambda-request-id'] = ctx.awsRequestId;
2730
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface HttpRequestContext {
1313
export interface APIGatewayEvent {
1414
path?: string,
1515
version?: number | string,
16+
cookies?: string[],
1617
queryStringParameters?: StringMap<string> | null,
1718
multiValueQueryStringParameters?: StringMap<string[]> | null,
1819
body?: string | null,

test/app/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ app.get('/cookies', (_, res) => {
2323
res.cookie('chocolate', '10');
2424
res.cookie('peanut_butter', '20');
2525
res.cookie('cinnamon', '30');
26+
res.cookie('hackyname', 'h3c/ky=;va{lu]e')
2627
res.send("cookies set");
2728
});
2829

test/eventToRequestOptions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('eventToRequestOptions', () => {
2323
"cloudfront-is-smarttv-viewer": "false",
2424
"cloudfront-is-tablet-viewer": "false",
2525
"cloudfront-viewer-country": "AU",
26-
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3",
26+
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De",
2727
"host": "apiid.execute-api.ap-southeast-2.amazonaws.com",
2828
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
2929
"via": "2.0 6e1c2492999626e81fa810e92ced9820.cloudfront.net (CloudFront)",
@@ -49,7 +49,7 @@ describe('eventToRequestOptions', () => {
4949
"headers": {
5050
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
5151
"accept-language": "en-US,en;q=0.9,pl-PL;q=0.8,pl;q=0.7,ru;q=0.6",
52-
"cookie": "cookie3=value3; cookie2=value2; cookie1=value1",
52+
"cookie": "cookie3=value3; cookie2=value2; cookie1=value1; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De",
5353
"connection": "keep-alive",
5454
"host": "elbname-234234234234.ap-southeast-2.elb.amazonaws.com",
5555
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",

test/fixtures/alb-event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"accept-encoding": "gzip, deflate",
1515
"accept-language": "en-US,en;q=0.9,pl-PL;q=0.8,pl;q=0.7,ru;q=0.6",
1616
"connection": "keep-alive",
17-
"cookie": "cookie3=value3; cookie2=value2; cookie1=value1",
17+
"cookie": "cookie3=value3; cookie2=value2; cookie1=value1; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De",
1818
"host": "elbname-234234234234.ap-southeast-2.elb.amazonaws.com",
1919
"upgrade-insecure-requests": "1",
2020
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",

test/fixtures/event-http-api-1.0.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"version": "1.0",
33
"resource": "$default",
4-
"path": "/cookies",
4+
"path": "/inspect",
55
"httpMethod": "GET",
66
"headers": {
77
"Content-Length": "0",
8-
"Host": "myapiid.execute-api.ap-southeast-2.amazonaws.com",
8+
"Host": "apiid.execute-api.ap-southeast-2.amazonaws.com",
99
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
1010
"X-Amzn-Trace-Id": "Root=1-5eac36bd-7a4863e4e05a7ca67c0a68ae",
11-
"X-Forwarded-For": "14.201.132.129",
11+
"X-Forwarded-For": "1.2.3.4, 4.5.6.7, 9.9.9.9",
1212
"X-Forwarded-Port": "443",
1313
"X-Forwarded-Proto": "https",
1414
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
@@ -18,14 +18,15 @@
1818
"sec-fetch-mode": "navigate",
1919
"sec-fetch-site": "none",
2020
"sec-fetch-user": "?1",
21-
"upgrade-insecure-requests": "1"
21+
"upgrade-insecure-requests": "1",
22+
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
2223
},
2324
"multiValueHeaders": {
2425
"Content-Length": [
2526
"0"
2627
],
2728
"Host": [
28-
"myapiid.execute-api.ap-southeast-2.amazonaws.com"
29+
"apiid.execute-api.ap-southeast-2.amazonaws.com"
2930
],
3031
"User-Agent": [
3132
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
@@ -34,7 +35,7 @@
3435
"Root=1-5eac36bd-7a4863e4e05a7ca67c0a68ae"
3536
],
3637
"X-Forwarded-For": [
37-
"14.201.132.129"
38+
"1.2.3.4, 4.5.6.7, 9.9.9.9"
3839
],
3940
"X-Forwarded-Port": [
4041
"443"
@@ -65,15 +66,20 @@
6566
],
6667
"upgrade-insecure-requests": [
6768
"1"
69+
],
70+
"cookie": [
71+
"cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
6872
]
6973
},
70-
"queryStringParameters": null,
74+
"queryStringParameters": {
75+
"param": "ab cd"
76+
},
7177
"multiValueQueryStringParameters": null,
7278
"requestContext": {
7379
"accountId": "356111732087",
74-
"apiId": "myapiid",
75-
"domainName": "myapiid.execute-api.ap-southeast-2.amazonaws.com",
76-
"domainPrefix": "myapiid",
80+
"apiId": "apiid",
81+
"domainName": "apiid.execute-api.ap-southeast-2.amazonaws.com",
82+
"domainPrefix": "apiid",
7783
"extendedRequestId": "L219niDmywMEJPQ=",
7884
"httpMethod": "GET",
7985
"identity": {
@@ -85,12 +91,12 @@
8591
"cognitoIdentityId": null,
8692
"cognitoIdentityPoolId": null,
8793
"principalOrgId": null,
88-
"sourceIp": "14.201.132.129",
94+
"sourceIp": "9.9.9.9",
8995
"user": null,
9096
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
9197
"userArn": null
9298
},
93-
"path": "/cookies",
99+
"path": "/inspect",
94100
"protocol": "HTTP/1.1",
95101
"requestId": "L219niDmywMEJPQ=",
96102
"requestTime": "01/May/2020:14:48:29 +0000",

test/fixtures/event-http-api-2.0.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
{
22
"version": "2.0",
33
"routeKey": "$default",
4-
"rawPath": "/inspects",
5-
"rawQueryString": "",
4+
"rawPath": "/inspect",
5+
"rawQueryString": "param=ab%20cd",
6+
"cookies": [
7+
"cookie1=value1",
8+
"cookie2=value2",
9+
"cookie3=value3",
10+
"hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
11+
],
612
"headers": {
713
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
814
"accept-encoding": "gzip, deflate, br",
915
"accept-language": "en-US,en;q=0.9,pl-PL;q=0.8,pl;q=0.7,ru;q=0.6",
1016
"cache-control": "max-age=0",
1117
"content-length": "0",
12-
"host": "myapiid.execute-api.ap-southeast-2.amazonaws.com",
18+
"host": "apiid.execute-api.ap-southeast-2.amazonaws.com",
1319
"sec-fetch-dest": "document",
1420
"sec-fetch-mode": "navigate",
1521
"sec-fetch-site": "none",
1622
"sec-fetch-user": "?1",
1723
"upgrade-insecure-requests": "1",
1824
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
1925
"x-amzn-trace-id": "Root=1-5eac37e3-8e447320ce745898566b3300",
20-
"x-forwarded-for": "123.123.123.123",
26+
"x-forwarded-for": "1.2.3.4, 4.5.6.7, 9.9.9.9",
2127
"x-forwarded-port": "443",
2228
"x-forwarded-proto": "https"
2329
},
30+
"queryStringParameters": {
31+
"param": "ab cd"
32+
},
2433
"requestContext": {
2534
"accountId": "356111732087",
26-
"apiId": "myapiid",
27-
"domainName": "myapiid.execute-api.ap-southeast-2.amazonaws.com",
28-
"domainPrefix": "myapiid",
35+
"apiId": "apiid",
36+
"domainName": "apiid.execute-api.ap-southeast-2.amazonaws.com",
37+
"domainPrefix": "apiid",
2938
"http": {
3039
"method": "GET",
31-
"path": "/cookies",
40+
"path": "/inspect",
3241
"protocol": "HTTP/1.1",
33-
"sourceIp": "123.123.123.123",
42+
"sourceIp": "9.9.9.9",
3443
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
3544
},
3645
"requestId": "L22rohfjSwMEMHA=",

test/fixtures/event-http-api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"X-Forwarded-Port": "443",
1212
"X-Forwarded-Proto": "https",
1313
"accept": "*/*",
14-
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3"
14+
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
1515
},
1616
"multiValueHeaders": {
1717
"Content-Length": [
@@ -39,7 +39,7 @@
3939
"*/*"
4040
],
4141
"cookie": [
42-
"cookie1=value1; cookie2=value2; cookie3=value3"
42+
"cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
4343
]
4444
},
4545
"queryStringParameters": {

test/fixtures/event.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"CloudFront-Is-SmartTV-Viewer": "false",
1313
"CloudFront-Is-Tablet-Viewer": "false",
1414
"CloudFront-Viewer-Country": "AU",
15-
"Cookie": "cookie1=value1; cookie2=value2; cookie3=value3",
15+
"Cookie": "cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De",
1616
"Host": "apiid.execute-api.ap-southeast-2.amazonaws.com",
1717
"upgrade-insecure-requests": "1",
1818
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
@@ -52,7 +52,7 @@
5252
"AU"
5353
],
5454
"Cookie": [
55-
"cookie1=value1; cookie2=value2; cookie3=value3"
55+
"cookie1=value1; cookie2=value2; cookie3=value3; hackyname=h3c%2Fky%3D%3Bva%7Blu%5De"
5656
],
5757
"Host": [
5858
"apiid.execute-api.ap-southeast-2.amazonaws.com"

test/integration.test.ts

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('integration', () => {
4242
'chocolate=10; Path=/',
4343
'peanut_butter=20; Path=/',
4444
'cinnamon=30; Path=/',
45+
'hackyname=h3c%2Fky%3D%3Bva%7Blu%5De; Path=/',
4546
]
4647
}));
4748
});
@@ -109,6 +110,7 @@ describe('integration', () => {
109110
cookie1: "value1",
110111
cookie2: "value2",
111112
cookie3: "value3",
113+
hackyname: "h3c/ky=;va{lu]e",
112114
},
113115
fresh: false,
114116
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
@@ -142,6 +144,7 @@ describe('integration', () => {
142144
expect(response.isBase64Encoded).toEqual(false);
143145
expect(response.multiValueHeaders!["content-type"][0]).toEqual('application/json; charset=utf-8');
144146
expect(response.multiValueHeaders!["x-powered-by"][0]).toEqual('Express');
147+
expect(response.headers).toBeUndefined()
145148
const json = JSON.parse(response.body);
146149
expect(json).toEqual({
147150
baseUrl: "",
@@ -150,6 +153,7 @@ describe('integration', () => {
150153
cookie1: "value1",
151154
cookie2: "value2",
152155
cookie3: "value3",
156+
hackyname: "h3c/ky=;va{lu]e",
153157
},
154158
fresh: false,
155159
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
@@ -181,33 +185,86 @@ describe('integration', () => {
181185
const response = await handler(eventHttpApiV1)
182186
expect(response.statusCode).toEqual(200);
183187
expect(response.isBase64Encoded).toEqual(false);
184-
expect(response.multiValueHeaders!["content-type"][0]).toEqual('text/html; charset=utf-8');
188+
expect(response.multiValueHeaders!["content-type"][0]).toEqual('application/json; charset=utf-8');
185189
expect(response.multiValueHeaders!["x-powered-by"][0]).toEqual('Express');
186-
expect(response.multiValueHeaders!["set-cookie"]).toEqual([
187-
'chocolate=10; Path=/',
188-
'peanut_butter=20; Path=/',
189-
'cinnamon=30; Path=/',
190-
])
191190
expect(response.headers).toBeUndefined()
192-
expect(response.cookies).toBeUndefined()
193-
expect(response.body).toEqual('cookies set');
191+
const json = JSON.parse(response.body);
192+
expect(json).toEqual({
193+
baseUrl: "",
194+
body: {},
195+
cookies: {
196+
cookie1: "value1",
197+
cookie2: "value2",
198+
cookie3: "value3",
199+
hackyname: "h3c/ky=;va{lu]e",
200+
},
201+
fresh: false,
202+
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
203+
ip: "4.5.6.7",
204+
ips: ["4.5.6.7", "9.9.9.9"],
205+
method: "GET",
206+
originalUrl: "/inspect?param=ab%20cd",
207+
params: {},
208+
path: "/inspect",
209+
protocol: "https",
210+
query: {
211+
param: "ab cd",
212+
},
213+
secure: true,
214+
signedCookies: {},
215+
stale: true,
216+
subdomains: [
217+
"ap-southeast-2",
218+
"execute-api",
219+
"apiid",
220+
],
221+
url: "/inspect?param=ab%20cd",
222+
xForwardedFor: "1.2.3.4, 4.5.6.7, 9.9.9.9",
223+
xhr: false,
224+
})
194225
})
195226

196227
it('handles HTTP API v2.0 event', async () => {
197228
const response = await handler(eventHttpApiV2)
198229
expect(response.statusCode).toEqual(200);
199230
expect(response.isBase64Encoded).toEqual(false);
200-
expect(response.headers!["content-type"]).toEqual('text/html; charset=utf-8');
231+
expect(response.headers!["content-type"]).toEqual('application/json; charset=utf-8');
201232
expect(response.headers!["x-powered-by"]).toEqual('Express');
202-
expect(response.headers!["set-cookie"]).toBeUndefined()
203233
expect(response.multiValueHeaders).toBeUndefined()
204-
expect(response.cookies).toEqual([
205-
'chocolate=10; Path=/',
206-
'peanut_butter=20; Path=/',
207-
'cinnamon=30; Path=/',
208-
])
209-
expect(response.body).toEqual('cookies set');
210-
234+
const json = JSON.parse(response.body);
235+
expect(json).toEqual({
236+
baseUrl: "",
237+
body: {},
238+
cookies: {
239+
cookie1: "value1",
240+
cookie2: "value2",
241+
cookie3: "value3",
242+
hackyname: "h3c/ky=;va{lu]e",
243+
},
244+
fresh: false,
245+
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
246+
ip: "4.5.6.7",
247+
ips: ["4.5.6.7", "9.9.9.9"],
248+
method: "GET",
249+
originalUrl: "/inspect?param=ab%20cd",
250+
params: {},
251+
path: "/inspect",
252+
protocol: "https",
253+
query: {
254+
param: "ab cd",
255+
},
256+
secure: true,
257+
signedCookies: {},
258+
stale: true,
259+
subdomains: [
260+
"ap-southeast-2",
261+
"execute-api",
262+
"apiid",
263+
],
264+
url: "/inspect?param=ab%20cd",
265+
xForwardedFor: "1.2.3.4, 4.5.6.7, 9.9.9.9",
266+
xhr: false,
267+
})
211268
})
212269

213270

@@ -225,6 +282,7 @@ describe('integration', () => {
225282
cookie1: "value1",
226283
cookie2: "value2",
227284
cookie3: "value3",
285+
hackyname: "h3c/ky=;va{lu]e",
228286
},
229287
fresh: false,
230288
hostname: "elbname-234234234234.ap-southeast-2.elb.amazonaws.com",

0 commit comments

Comments
 (0)