/
createLambdaProxyContextTest.js
501 lines (401 loc) · 19.8 KB
/
createLambdaProxyContextTest.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
const { expect } = require('chai');
const RequestBuilder = require('../support/RequestBuilder');
const createLambdaProxyContext = require('../../src/createLambdaProxyContext');
describe('createLambdaProxyContext', () => {
const expectFixedAttributes = lambdaProxyContext => {
const requestContext = lambdaProxyContext.requestContext;
expect(requestContext.accountId).to.eq('offlineContext_accountId');
expect(requestContext.resourceId).to.eq('offlineContext_resourceId');
expect(requestContext.identity.cognitoIdentityPoolId).to.eq('offlineContext_cognitoIdentityPoolId');
expect(requestContext.identity.accountId).to.eq('offlineContext_accountId');
expect(requestContext.identity.cognitoIdentityId).to.eq('offlineContext_cognitoIdentityId');
expect(requestContext.identity.caller).to.eq('offlineContext_caller');
expect(requestContext.identity.apiKey).to.eq('offlineContext_apiKey');
expect(requestContext.identity.cognitoAuthenticationType).to.eq('offlineContext_cognitoAuthenticationType');
expect(requestContext.identity.cognitoAuthenticationProvider).to.eq('offlineContext_cognitoAuthenticationProvider');
expect(requestContext.identity.userArn).to.eq('offlineContext_userArn');
expect(requestContext.identity.user).to.eq('offlineContext_user');
expect(requestContext.authorizer.principalId).to.eq('offlineContext_authorizer_principalId');
expect(requestContext.requestTimeEpoch).to.eq(1);
};
const stageVariables = {};
const options = {
stage: 'dev',
};
context('with a GET /fn1 request', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('queryStringParameters should be null', () => {
expect(lambdaProxyContext.queryStringParameters).to.be.null;
});
it('pathParameters should be null', () => {
expect(lambdaProxyContext.pathParameters).to.be.null;
});
it('httpMethod should be GET', () => {
expect(lambdaProxyContext.httpMethod).to.eq('GET');
});
it('body should be null', () => {
expect(lambdaProxyContext.body).to.be.null;
});
it('should have a unique requestId', () => {
const prefix = 'offlineContext_requestId_';
expect(lambdaProxyContext.requestContext.requestId.length).to.be.greaterThan(prefix.length);
const randomNumber = +lambdaProxyContext.requestContext.requestId.slice(prefix.length);
expect(randomNumber).to.be.a('number');
});
it('should match fixed attributes', () => {
expectFixedAttributes(lambdaProxyContext);
});
});
context('with a GET /fn1 request with headers', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
requestBuilder.addHeader('Content-Type', 'application/json');
requestBuilder.addHeader('Authorization', 'Token token="1234567890"');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have two headers', () => {
expect(Object.keys(lambdaProxyContext.headers).length).to.eq(2);
expect(lambdaProxyContext.headers['Content-Type']).to.eq('application/json');
expect(lambdaProxyContext.headers.Authorization).to.eq('Token token="1234567890"');
});
it('should not have claims for authorizer if token is not JWT', () => {
expect(lambdaProxyContext.requestContext.authorizer.claims).to.be.undefined;
});
});
context('with a GET /fn1 request with Authorization header that contains JWT token', () => {
// mock token
// header
// {
// "alg": "HS256",
// "typ": "JWT"
// }
// payload
// {
// "sub": "1234567890",
// "name": "John Doe",
// "admin": true
// }
/* eslint-disable max-len */
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';
const bearerToken = `Bearer ${token}`;
it('should have claims for authorizer if Authorization header has valid JWT', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
requestBuilder.addHeader('Authorization', token);
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.requestContext.authorizer.claims).to.deep.equal({
sub: '1234567890',
name: 'John Doe',
admin: true,
});
});
it('should have claims for authorizer if authorization header has valid JWT', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
requestBuilder.addHeader('authorization', token);
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.requestContext.authorizer.claims).to.deep.equal({
sub: '1234567890',
name: 'John Doe',
admin: true,
});
});
it('should have claims for authorizer if Authorization header has valid Bearer JWT', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
requestBuilder.addHeader('Authorization', bearerToken);
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.requestContext.authorizer.claims).to.deep.equal({
sub: '1234567890',
name: 'John Doe',
admin: true,
});
});
it('should have claims for authorizer if authorization header has valid Bearer JWT', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
requestBuilder.addHeader('authorization', bearerToken);
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.requestContext.authorizer.claims).to.deep.equal({
sub: '1234567890',
name: 'John Doe',
admin: true,
});
});
});
context('with a POST /fn1 request with no headers', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should calculate the Content-Length header', () => {
expect(lambdaProxyContext.headers['Content-Length']).to.eq(15);
});
it('should inject a default Content-Type header', () => {
expect(lambdaProxyContext.headers['Content-Type']).to.eq('application/json');
});
it('should stringify the payload for the body', () => {
expect(lambdaProxyContext.body).to.eq('{"key":"value"}');
});
it('should not have claims for authorizer', () => {
expect(lambdaProxyContext.requestContext.authorizer.claims).to.be.undefined;
});
});
context('with a POST /fn1 request with a lowercase Content-Type header', () => {
it('should assign the value to Content-Type', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('content-type', 'custom/test');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers['content-type']).to.eq('custom/test');
});
});
context('with a POST /fn1 request with a single content-type header', () => {
it('should not assign the value to Content-Type', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('content-type', 'custom/test');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers['Content-Type']).to.eq(undefined);
});
});
context('with a POST /fn1 request with a accept header', () => {
it('should assign the value to accept', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('accept', 'custom/test');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers.accept).to.eq('custom/test');
});
});
context('with a POST /fn1 request with a camelcase Content-Type header', () => {
it('should assign the value to Content-Type', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('Content-Type', 'custom/test');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers['Content-Type']).to.eq('custom/test');
});
});
context('with a POST /fn1 request with a set Content-length', () => {
it('should have one content-length header only', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('content-type', 'custom/test');
requestBuilder.addHeader('content-length', '2');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(Object.keys(lambdaProxyContext.headers).filter(header => header === 'content-length')).to.have.lengthOf(1);
});
});
context('with a POST /fn1 request with a set Content-length', () => {
it('should have one content-length header only', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('content-type', 'custom/test');
requestBuilder.addHeader('Content-length', '2');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(Object.keys(lambdaProxyContext.headers).filter(header => header.toLowerCase() === 'content-length')).to.have.lengthOf(1);
});
});
context('with a POST /fn1 request with a X-GitHub-Event header', () => {
it('should assign not change the header case', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('X-GitHub-Event', 'test');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers['X-GitHub-Event']).to.eq('test');
expect(lambdaProxyContext.multiValueHeaders['X-GitHub-Event']).to.deep.eq(['test']);
});
});
context('with a POST /fn1 request with multiValueHeaders', () => {
it('should assign not change the header case', () => {
const requestBuilder = new RequestBuilder('POST', '/fn1');
requestBuilder.addBody({ key: 'value' });
requestBuilder.addHeader('Some-Header', 'test1');
requestBuilder.addHeader('Some-Header', 'test2');
const request = requestBuilder.toObject();
const lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
expect(lambdaProxyContext.headers['Some-Header']).to.eq('test2');
expect(lambdaProxyContext.multiValueHeaders['Some-Header']).to.deep.eq(['test1', 'test2']);
});
});
context('with a GET /fn1/{id} request with path parameters', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1/1234');
requestBuilder.addParam('id', '1234');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have a path parameter', () => {
expect(Object.keys(lambdaProxyContext.pathParameters).length).to.eq(1);
expect(lambdaProxyContext.pathParameters.id).to.eq('1234');
});
});
context('with a GET /fn1/{id} request with encoded path parameters', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1/test|1234');
requestBuilder.addParam('id', 'test|1234');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have a path parameter', () => {
expect(Object.keys(lambdaProxyContext.pathParameters).length).to.eq(1);
expect(lambdaProxyContext.pathParameters.id).to.eq('test|1234');
});
});
context('with a GET /fn1?param=1 request with single parameter in query string', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1?param=1');
requestBuilder.addQuery('param', '1');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have a query parameter named param', () => {
expect(Object.keys(lambdaProxyContext.queryStringParameters).length).to.eq(1);
expect(lambdaProxyContext.queryStringParameters.param).to.eq('1');
});
it('should have a multi value query parameter', () => {
expect(Array.isArray(lambdaProxyContext.multiValueQueryStringParameters.param)).to.eq(true);
expect(lambdaProxyContext.multiValueQueryStringParameters.param[0]).to.eq('1');
});
});
context('with a GET /fn1?param=1¶m2=1 request with double parameters in query string', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1?param=1');
requestBuilder.addQuery('param', '1');
requestBuilder.addQuery('param2', '1');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have a two query parameters', () => {
expect(Object.keys(lambdaProxyContext.queryStringParameters).length).to.eq(2);
expect(lambdaProxyContext.queryStringParameters.param).to.eq('1');
expect(lambdaProxyContext.queryStringParameters.param2).to.eq('1');
});
});
context('with a GET /fn1?param=1¶m=1 request with single query string', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1?param=1');
// emaulate HAPI `query` as described here:
// https://futurestud.io/tutorials/hapi-how-to-use-query-parameters#multiplequeryparametersofthesamename
requestBuilder.addQuery('param', ['1', '2']);
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have a two query parameters', () => {
expect(Object.keys(lambdaProxyContext.queryStringParameters).length).to.eq(1);
expect(lambdaProxyContext.queryStringParameters.param).to.eq('2');
});
});
context('with a GET /fn1?param=1¶m=2 request with multiValueQueryStringParameters', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1?param=1¶m=2');
// emaulate HAPI `query` as described here:
// https://futurestud.io/tutorials/hapi-how-to-use-query-parameters#multiplequeryparametersofthesamename
requestBuilder.addQuery('param', ['1', '2']);
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('multi value param should have a two values', () => {
expect(Object.keys(lambdaProxyContext.multiValueQueryStringParameters).length).to.eq(1);
expect(lambdaProxyContext.multiValueQueryStringParameters.param.length).to.eq(2);
});
});
context('with a request that includes cognito-identity-id header', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
const testId = 'test-id';
requestBuilder.addHeader('cognito-identity-id', testId);
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have the expected cognitoIdentityId', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoIdentityId).to.eq(testId);
});
it('should have the expected headers', () => {
expect(Object.keys(lambdaProxyContext.headers).length).to.eq(1);
expect(lambdaProxyContext.headers['cognito-identity-id']).to.eq(testId);
});
});
context('with a request that includes cognito-authentication-provider header', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
const testId = 'lorem:ipsum:test-id';
requestBuilder.addHeader('cognito-authentication-provider', testId);
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have the expected cognitoAuthenticationProvider', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoAuthenticationProvider).to.eq(testId);
});
it('should have the expected headers', () => {
expect(Object.keys(lambdaProxyContext.headers).length).to.eq(1);
expect(lambdaProxyContext.headers['cognito-authentication-provider']).to.eq(testId);
});
});
context('with environment variables', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1');
const request = requestBuilder.toObject();
let lambdaProxyContext;
before(() => {
process.env.SLS_COGNITO_IDENTITY_POOL_ID = 'customCognitoIdentityPoolId';
process.env.SLS_ACCOUNT_ID = 'customAccountId';
process.env.SLS_COGNITO_IDENTITY_ID = 'customCognitoIdentityId';
process.env.SLS_CALLER = 'customCaller';
process.env.SLS_API_KEY = 'customApiKey';
process.env.SLS_COGNITO_AUTHENTICATION_TYPE = 'customCognitoAuthenticationType';
process.env.SLS_COGNITO_AUTHENTICATION_PROVIDER = 'customCognitoAuthenticationProvider';
lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
});
it('should have the expected cognitoIdentityPoolId', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoIdentityPoolId)
.to.eq('customCognitoIdentityPoolId');
});
it('should have the expected accountId', () => {
expect(lambdaProxyContext.requestContext.identity.accountId).to.eq('customAccountId');
});
it('should have the expected cognitoIdentityId', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoIdentityId)
.to.eq('customCognitoIdentityId');
});
it('should have the expected caller', () => {
expect(lambdaProxyContext.requestContext.identity.caller).to.eq('customCaller');
});
it('should have the expected apiKey', () => {
expect(lambdaProxyContext.requestContext.identity.apiKey).to.eq('customApiKey');
});
it('should have the expected cognitoAuthenticationType', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoAuthenticationType)
.to.eq('customCognitoAuthenticationType');
});
it('should have the expected cognitoAuthenticationProvider', () => {
expect(lambdaProxyContext.requestContext.identity.cognitoAuthenticationProvider)
.to.eq('customCognitoAuthenticationProvider');
});
});
});