Skip to content

Commit 9be5ee3

Browse files
committed
fix(newman): fixed dev server express mapping
1 parent 2530e38 commit 9be5ee3

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

dev-server.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let server = null;
1010

1111
app.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,12 +20,12 @@ app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
2020
})
2121

2222
app.all('/api', async (req, res) => {
23-
handler(req, {}).then(result => {
23+
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
2424
res.send(result);
2525
}).catch(error => console.error(error));
2626
});
2727
app.all('/__health', async (req, res) => {
28-
handler(req, {}).then(result => {
28+
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
2929
res.send(result);
3030
}).catch(error => console.error(error));
3131
});
@@ -39,8 +39,8 @@ server = app.listen(port, () => {
3939
if (process.argv[2]) {
4040
const testFilename = process.argv[3] || 'service-collection.postman_collection.json'
4141

42-
console.log('Running Newman tests');
4342
if (!existsSync(`./${testFilename}`)) {
43+
console.log('Running Newman tests');
4444
console.error(`Error: test collection not found
4545
./${testFilename}
4646
@@ -63,4 +63,86 @@ server = app.listen(port, () => {
6363
process.exit();
6464
})
6565
}
66-
})
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

Comments
 (0)