-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
handler.test.ts
171 lines (163 loc) · 5.65 KB
/
handler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import type { LambdaEvent } from './handler'
import { getProcessor, isContentEncodingBinary, isContentTypeBinary } from './handler'
describe('isContentTypeBinary', () => {
it('Should determine whether it is binary', () => {
expect(isContentTypeBinary('image/png')).toBe(true)
expect(isContentTypeBinary('font/woff2')).toBe(true)
expect(isContentTypeBinary('image/svg+xml')).toBe(false)
expect(isContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('text/plain')).toBe(false)
expect(isContentTypeBinary('text/plain; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('text/css')).toBe(false)
expect(isContentTypeBinary('text/javascript')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
expect(isContentTypeBinary('application/ld+json')).toBe(false)
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
})
})
describe('isContentEncodingBinary', () => {
it('Should determine whether it is compressed', () => {
expect(isContentEncodingBinary('gzip')).toBe(true)
expect(isContentEncodingBinary('compress')).toBe(true)
expect(isContentEncodingBinary('deflate')).toBe(true)
expect(isContentEncodingBinary('br')).toBe(true)
expect(isContentEncodingBinary('deflate, gzip')).toBe(true)
expect(isContentEncodingBinary('')).toBe(false)
expect(isContentEncodingBinary('unknown')).toBe(false)
})
})
describe('EventProcessor.createRequest', () => {
it('Should return valid Request object from version 1.0 API Gateway event', () => {
const event: LambdaEvent = {
version: '1.0',
resource: '/my/path',
path: '/my/path',
httpMethod: 'GET',
headers: {
'content-type': 'application/json',
header1: 'value1',
header2: 'value1',
},
multiValueHeaders: {
header1: ['value1'],
header2: ['value1', 'value2', 'value3'],
},
// This value doesn't match multi value's content.
// We want to assert handler is using the multi value's content when both are available.
queryStringParameters: {
parameter2: 'value',
},
multiValueQueryStringParameters: {
parameter1: ['value1', 'value2'],
parameter2: ['value'],
},
requestContext: {
accountId: '123456789012',
apiId: 'id',
authorizer: {
claims: null,
scopes: null,
},
domainName: 'id.execute-api.us-east-1.amazonaws.com',
domainPrefix: 'id',
extendedRequestId: 'request-id',
httpMethod: 'GET',
identity: {
sourceIp: '192.0.2.1',
userAgent: 'user-agent',
clientCert: {
clientCertPem: 'CERT_CONTENT',
subjectDN: 'www.example.com',
issuerDN: 'Example issuer',
serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',
validity: {
notBefore: 'May 28 12:30:02 2019 GMT',
notAfter: 'Aug 5 09:36:04 2021 GMT',
},
},
},
path: '/my/path',
protocol: 'HTTP/1.1',
requestId: 'id=',
requestTime: '04/Mar/2020:19:15:17 +0000',
requestTimeEpoch: 1583349317135,
resourcePath: '/my/path',
stage: '$default',
},
pathParameters: {},
stageVariables: {},
body: null,
isBase64Encoded: false,
}
const processor = getProcessor(event)
const request = processor.createRequest(event)
expect(request.method).toEqual('GET')
expect(request.url).toEqual(
'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'
)
expect(Object.fromEntries(request.headers)).toEqual({
'content-type': 'application/json',
header1: 'value1',
header2: 'value1, value2, value3',
})
})
it('Should return valid Request object from version 2.0 API Gateway event', () => {
const event: LambdaEvent = {
version: '2.0',
routeKey: '$default',
rawPath: '/my/path',
rawQueryString: 'parameter1=value1¶meter1=value2¶meter2=value',
cookies: ['cookie1', 'cookie2'],
headers: {
'content-type': 'application/json',
header1: 'value1',
header2: 'value1,value2',
},
queryStringParameters: {
parameter1: 'value1,value2',
parameter2: 'value',
},
requestContext: {
accountId: '123456789012',
apiId: 'api-id',
authentication: null,
authorizer: {},
domainName: 'id.execute-api.us-east-1.amazonaws.com',
domainPrefix: 'id',
http: {
method: 'POST',
path: '/my/path',
protocol: 'HTTP/1.1',
sourceIp: '192.0.2.1',
userAgent: 'agent',
},
requestId: 'id',
routeKey: '$default',
stage: '$default',
time: '12/Mar/2020:19:03:58 +0000',
timeEpoch: 1583348638390,
},
body: 'Hello from Lambda',
pathParameters: {
parameter1: 'value1',
},
isBase64Encoded: false,
stageVariables: {
stageVariable1: 'value1',
stageVariable2: 'value2',
},
}
const processor = getProcessor(event)
const request = processor.createRequest(event)
expect(request.method).toEqual('POST')
expect(request.url).toEqual(
'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'
)
expect(Object.fromEntries(request.headers)).toEqual({
'content-type': 'application/json',
cookie: 'cookie1; cookie2',
header1: 'value1',
header2: 'value1,value2',
})
})
})