@@ -4,7 +4,26 @@ import {
44 ApplicationLoadBalancerEventRequestContext ,
55 APIGatewayRequestEvent ,
66 ApplicationLoadBalancerRequestEvent } from '../src/request-response-types' ;
7- import { Context } from 'aws-lambda' ;
7+ import { APIGatewayEventIdentity , Context } from 'aws-lambda' ;
8+ import withDefault from './test-utils/withDefault' ;
9+
10+ interface MakeAPIGatewayRequestEventContextParams {
11+ httpMethod ?: string | null ;
12+ }
13+ interface MakeAPIGatewayRequestEventHeadersParams {
14+ [ name : string ] : string | undefined ;
15+ }
16+
17+ interface MakeAPIGatewayRequestEventMultiValueHeadersParams {
18+ [ name : string ] : string [ ] | undefined ;
19+ }
20+
21+ interface MakeAPIGatewayRequestEventParams {
22+ httpMethod ?: string ;
23+ path ?: string ;
24+ headers ?: MakeAPIGatewayRequestEventHeadersParams ;
25+ multiValueHeaders ?: MakeAPIGatewayRequestEventMultiValueHeadersParams ;
26+ }
827
928export const handlerContext = ( fillAllFields : boolean = false ) : Context => {
1029 let ctx : Context ;
@@ -215,3 +234,118 @@ export const albMultiValHeadersRequest = (): ApplicationLoadBalancerRequestEvent
215234 } ,
216235 } ) ;
217236} ;
237+
238+ export const makeAPIGatewayRequestContextIdentity = ( ) : APIGatewayEventIdentity => {
239+ return {
240+ accessKey : null ,
241+ accountId : null ,
242+ apiKey : null ,
243+ apiKeyId : null ,
244+ caller : null ,
245+ clientCert : null ,
246+ cognitoAuthenticationProvider : null ,
247+ cognitoAuthenticationType : null ,
248+ cognitoIdentityId : null ,
249+ cognitoIdentityPoolId : null ,
250+ principalOrgId : null ,
251+ sourceIp : '12.12.12.12' ,
252+ user : null ,
253+ userAgent : 'curl/7.54.0' ,
254+ userArn : null ,
255+ } ;
256+ } ;
257+
258+ export const makeAPIGatewayRequestContext = ( params ?: MakeAPIGatewayRequestEventContextParams ) : APIGatewayEventRequestContext => {
259+ return {
260+ accountId : '123456789012' ,
261+ apiId : 'someapi' ,
262+ authorizer : null ,
263+ httpMethod : params ?. httpMethod ?? 'GET' ,
264+ path : '/prd' ,
265+ protocol : 'HTTP/1.1' ,
266+ stage : 'prd' ,
267+ requestId : 'a507736b-259e-11e9-8fcf-4f1f08c4591e' ,
268+ requestTimeEpoch : 1548969891530 ,
269+ resourceId : 'reas23acc' ,
270+ identity : makeAPIGatewayRequestContextIdentity ( ) ,
271+ resourcePath : '/' ,
272+ } ;
273+ } ;
274+
275+ export const makeAPIGatewayRequestEventHeaders =
276+ ( params ?: MakeAPIGatewayRequestEventHeadersParams ) : APIGatewayRequestEvent [ 'headers' ] => {
277+ return withDefault < APIGatewayRequestEvent [ 'headers' ] > (
278+ params ,
279+ {
280+ Accept : '*/*' ,
281+ 'CloudFront-Forwarded-Proto' : 'https' ,
282+ 'CloudFront-Is-Desktop-Viewer' : 'true' ,
283+ 'CloudFront-Is-Mobile-Viewer' : 'false' ,
284+ 'CloudFront-Is-SmartTV-Viewer' : 'false' ,
285+ 'CloudFront-Is-Tablet-Viewer' : 'false' ,
286+ 'CloudFront-Viewer-Country' : 'US' ,
287+ Host : 'b5gee6dacf.execute-api.us-east-1.amazonaws.com' ,
288+ 'User-Agent' : 'curl/7.54.0' ,
289+ Via : '2.0 4ee511e558a0400aa4b9c1d34d92af5a.cloudfront.net (CloudFront)' ,
290+ 'X-Amz-Cf-Id' : 'xn-ohXlUAed-32bae2cfb7164fd690ffffb87d36b032==' ,
291+ 'X-Amzn-Trace-Id' : 'Root=1-4b5398e2-a7fbe4f92f2e911013cba76b' ,
292+ 'X-Forwarded-For' : '8.8.8.8, 2.3.4.5' ,
293+ 'X-Forwarded-Port' : '443' ,
294+ 'X-Forwarded-Proto' : 'https' ,
295+ Referer : 'https://en.wikipedia.org/wiki/HTTP_referer' ,
296+ Cookie : 'uid=abc; ga=1234; foo=bar; baz=foo%5Ba%5D; obj=j%3A%7B%22abc%22%3A123%7D; onechar=j; bad=j%3A%7Ba%7D' ,
297+ }
298+ ) ;
299+ } ;
300+
301+ export const makeAPIGatewayRequestEventMultiValueHeader =
302+ ( params ?: MakeAPIGatewayRequestEventMultiValueHeadersParams ) : APIGatewayRequestEvent [ 'multiValueHeaders' ] => {
303+ return withDefault < APIGatewayRequestEvent [ 'multiValueHeaders' ] > (
304+ params ,
305+ {
306+ Foo : [ 'bar' , 'baz' ] ,
307+ ...Object . fromEntries (
308+ Object . entries ( makeAPIGatewayRequestEventHeaders ( ) )
309+ . map ( ( [ key , value ] ) : Array < string | Array < string | undefined > > => {
310+ return [ key , [ value ] ] ;
311+ } )
312+ ) ,
313+ }
314+ ) ;
315+ } ;
316+
317+ export const makeAPIGatewayRequestEvent = ( params ?: MakeAPIGatewayRequestEventParams ) : APIGatewayRequestEvent => {
318+ const httpMethod = withDefault ( params ?. httpMethod , 'GET' ) ,
319+ path = withDefault ( params ?. path , '/echo/asdf/a' ) ,
320+ headers = withDefault ( params ?. headers , makeAPIGatewayRequestEventHeaders ( ) ) ;
321+
322+ const multiValueHeaders = withDefault (
323+ params ?. multiValueHeaders ,
324+ makeAPIGatewayRequestEventMultiValueHeader ( )
325+ ) ;
326+
327+ return {
328+ path,
329+ httpMethod,
330+ body : null ,
331+ isBase64Encoded : false ,
332+ resource : '/{proxy+}' ,
333+ pathParameters : { proxy : path } ,
334+ stageVariables : null ,
335+ requestContext : makeAPIGatewayRequestContext ( {
336+ httpMethod,
337+ } ) ,
338+ headers,
339+ multiValueHeaders,
340+ queryStringParameters : {
341+ 'foo[a]' : 'bar b' ,
342+ x : '2' ,
343+ y : 'z' ,
344+ } ,
345+ multiValueQueryStringParameters : {
346+ 'foo[a]' : [ 'bar b' , 'baz c' ] ,
347+ x : [ '1' , '2' ] ,
348+ y : [ 'z' ] ,
349+ } ,
350+ } ;
351+ } ;
0 commit comments