-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
148 lines (137 loc) · 3.77 KB
/
dev-server.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
const express = require('express')
const { existsSync } = require('fs')
const { handler } = require('./build/src/index')
const newman = require('newman')
const app = express();
const port = 9000;
let server = null;
app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
try {
const body = req.body ? JSON.parse(req.body) : ExpressToAWSAPIGatewayProxyEventV2(req);
handler(body, {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
} catch(error) {
console.error(error);
}
})
app.all('/api', async (req, res) => {
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
});
app.all('/__health', async (req, res) => {
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
res.send(result);
}).catch(error => console.error(error));
});
server = app.listen(port, () => {
console.log(`
Example app listening at:
POST http://localhost:${port}/2015-03-31/functions/function/invocations
`);
if (process.argv[2]) {
const testFilename = process.argv[3] || 'service-collection.postman_collection.json'
if (!existsSync(`./${testFilename}`)) {
console.log('Running Newman tests');
console.error(`Error: test collection not found
./${testFilename}
Skipping tests. Running dev server only.
`);
return;
}
console.log('Test collection:', testFilename);
newman.run({
collection: require(`./${testFilename}`),
reporters: 'cli',
},
(err, summary) => {
if (err) {
console.log(err);
process.exit(1)
}
process.exit();
})
}
})
function ExpressToAWSAPIGatewayProxyEventV2(request) {
return {
"resource": request.path,
"path": request.path,
"httpMethod": request.method,
"headers": {
"header1": "value1",
"header2": "value2"
},
"multiValueHeaders": {
"header1": [
"value1"
],
"header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1",
"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": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "IP",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"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,
"resourceId": null,
"resourcePath": "/my/path",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": false
}
}