11const express = require ( 'express' )
22const { existsSync } = require ( 'fs' )
3- const { handler } = require ( './app /index' )
3+ const { handler } = require ( './build/src /index' )
44const newman = require ( 'newman' )
55
66const app = express ( ) ;
@@ -10,7 +10,7 @@ let server = null;
1010
1111app . post ( '/2015-03-31/functions/function/invocations' , async ( req , res ) => {
1212 try {
13- const body = req . body ? JSON . parse ( req . body ) : req ;
13+ const body = req . body ? JSON . parse ( req . body ) : ExpressToAWSAPIGatewayProxyEventV2 ( req ) ;
1414 handler ( body , { } ) . then ( result => {
1515 res . send ( result ) ;
1616 } ) . catch ( error => console . error ( error ) ) ;
@@ -20,7 +20,12 @@ app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
2020} )
2121
2222app . all ( '/api' , async ( req , res ) => {
23- handler ( req , { } ) . then ( result => {
23+ handler ( ExpressToAWSAPIGatewayProxyEventV2 ( req ) , { } ) . then ( result => {
24+ res . send ( result ) ;
25+ } ) . catch ( error => console . error ( error ) ) ;
26+ } ) ;
27+ app . all ( '/__health' , async ( req , res ) => {
28+ handler ( ExpressToAWSAPIGatewayProxyEventV2 ( req ) , { } ) . then ( result => {
2429 res . send ( result ) ;
2530 } ) . catch ( error => console . error ( error ) ) ;
2631} ) ;
@@ -34,8 +39,8 @@ server = app.listen(port, () => {
3439 if ( process . argv [ 2 ] ) {
3540 const testFilename = process . argv [ 3 ] || 'service-collection.postman_collection.json'
3641
37- console . log ( 'Running Newman tests' ) ;
3842 if ( ! existsSync ( `./${ testFilename } ` ) ) {
43+ console . log ( 'Running Newman tests' ) ;
3944 console . error ( `Error: test collection not found
4045 ./${ testFilename }
4146
@@ -58,4 +63,86 @@ server = app.listen(port, () => {
5863 process . exit ( ) ;
5964 } )
6065 }
61- } )
66+ } )
67+
68+ function ExpressToAWSAPIGatewayProxyEventV2 ( request ) {
69+ return {
70+ "resource" : request . path ,
71+ "path" : request . path ,
72+ "httpMethod" : request . method ,
73+ "headers" : {
74+ "header1" : "value1" ,
75+ "header2" : "value2"
76+ } ,
77+ "multiValueHeaders" : {
78+ "header1" : [
79+ "value1"
80+ ] ,
81+ "header2" : [
82+ "value1" ,
83+ "value2"
84+ ]
85+ } ,
86+ "queryStringParameters" : {
87+ "parameter1" : "value1" ,
88+ "parameter2" : "value"
89+ } ,
90+ "multiValueQueryStringParameters" : {
91+ "parameter1" : [
92+ "value1" ,
93+ "value2"
94+ ] ,
95+ "parameter2" : [
96+ "value"
97+ ]
98+ } ,
99+ "requestContext" : {
100+ "accountId" : "123456789012" ,
101+ "apiId" : "id" ,
102+ "authorizer" : {
103+ "claims" : null ,
104+ "scopes" : null
105+ } ,
106+ "domainName" : "id.execute-api.us-east-1.amazonaws.com" ,
107+ "domainPrefix" : "id" ,
108+ "extendedRequestId" : "request-id" ,
109+ "httpMethod" : "GET" ,
110+ "identity" : {
111+ "accessKey" : null ,
112+ "accountId" : null ,
113+ "caller" : null ,
114+ "cognitoAuthenticationProvider" : null ,
115+ "cognitoAuthenticationType" : null ,
116+ "cognitoIdentityId" : null ,
117+ "cognitoIdentityPoolId" : null ,
118+ "principalOrgId" : null ,
119+ "sourceIp" : "IP" ,
120+ "user" : null ,
121+ "userAgent" : "user-agent" ,
122+ "userArn" : null ,
123+ "clientCert" : {
124+ "clientCertPem" : "CERT_CONTENT" ,
125+ "subjectDN" : "www.example.com" ,
126+ "issuerDN" : "Example issuer" ,
127+ "serialNumber" : "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1" ,
128+ "validity" : {
129+ "notBefore" : "May 28 12:30:02 2019 GMT" ,
130+ "notAfter" : "Aug 5 09:36:04 2021 GMT"
131+ }
132+ }
133+ } ,
134+ "path" : "/my/path" ,
135+ "protocol" : "HTTP/1.1" ,
136+ "requestId" : "id=" ,
137+ "requestTime" : "04/Mar/2020:19:15:17 +0000" ,
138+ "requestTimeEpoch" : 1583349317135 ,
139+ "resourceId" : null ,
140+ "resourcePath" : "/my/path" ,
141+ "stage" : "$default"
142+ } ,
143+ "pathParameters" : null ,
144+ "stageVariables" : null ,
145+ "body" : "Hello from Lambda!" ,
146+ "isBase64Encoded" : false
147+ }
148+ }
0 commit comments