-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
318 lines (277 loc) · 8.93 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { SMTPServer } = require('smtp-server');
const crypto = require('crypto');
const app = express();
const http = require('http');
const https = require('https');
const { readFileSync } = require('fs');
const HTTP_PORT = process.env.MOCK_HTTP_PORT || process.env.PORT || 1080;
const HTTPS_PORT = process.env.MOCK_HTTPS_PORT || process.env.HTTPS_PORT;
const SMTP_PORT = process.env.MOCK_SMTP_PORT || process.env.SMTP_PORT || '25';
const HOST = process.env.MOCK_HOST || process.env.HOST || '0.0.0.0';
const MOCK_PATH = process.env.MOCK_PATH || '/mock';
let routes = [];
let calls = [];
let nextCallListeners = [];
let mails = [];
let nextMailListeners = [];
let recordingsContext = {
active: false,
deleteBodyAttributesForHash: [],
forwardHeadersForRoute: [],
deleteHeadersForHash: [],
recordings: {},
};
/*
Structure of route:
{
request: {
match: ''
bodyMatch: ''
},
response: {
status: 200,
body: { ... },
}
}
*/
app.use(cors({ origin: '*' }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.text({ limit: '50mb', type: '*/*' }));
const smtpServer = new SMTPServer({
authOptional: true,
onData: async (stream, _session, callback) => {
console.log('Got mail');
const mail = await streamToString(stream);
if (nextMailListeners.length) {
console.log('Notifying mail listeners', nextMailListeners.length);
nextMailListeners.forEach((listener) => listener(mail));
nextMailListeners = [];
} else {
console.log('Adding mail to queue');
mails.push(mail);
}
callback();
},
onAuth: (_auth, _session, cb) => {
console.log('Mail auth');
cb(null, { user: 'dummy' });
},
});
smtpServer.listen(SMTP_PORT, HOST);
const route = express.Router();
app.use(MOCK_PATH, route);
const streamToString = (readStream) =>
new Promise((res) => {
const chunks = [];
readStream.on('data', (chunk) => chunks.push(chunk));
readStream.on('end', () => res(Buffer.concat(chunks)));
});
route.post('/mock', (req, res) => {
console.log(`Mocking route:`, req.body);
routes = routes.filter(
(route) => !(route.request.match === req.body.request.match && route.request.bodyMatch === req.body.request.bodyMatch)
);
routes.push(req.body);
res.sendStatus(204);
});
route.post('/reset', (_req, res) => {
console.log('resetting');
routes = [];
calls = [];
nextCallListeners = [];
mails = [];
nextMailListeners = [];
res.sendStatus(204);
});
route.post('/reset/calls', (_req, res) => {
console.log('resetting calls');
calls = [];
nextCallListeners = [];
mails = [];
nextMailListeners = [];
res.sendStatus(204);
});
route.post('/recordings', (req, res) => {
console.log('Setting up recordings... info:', req.body);
recordingsContext.active = req.body.active || false;
recordingsContext.deleteBodyAttributesForHash = req.body.deleteBodyAttributesForHash || [];
recordingsContext.forwardHeadersForRoute = req.body.forwardHeadersForRoute || [];
recordingsContext.recordings = req.body.recordings || {};
recordingsContext.deleteHeadersForHash = req.body.deleteHeadersForHash || [];
res.sendStatus(204);
});
route.get('/recordings', (_, res) => {
console.log('Getting recordings');
res.send(recordingsContext.recordings);
});
route.get('/calls', (_req, res) => {
res.send(calls);
});
route.get('/calls/next', (_req, res) => {
if (calls.length) {
console.log('sending a call');
res.send(calls.shift());
} else {
console.log('registering a call listener');
nextCallListeners.push((call) => res.send(call));
}
});
route.get('/mails', (_req, res) => {
res.send(mails);
});
route.get('/mails/next', (_req, res) => {
if (mails.length) {
console.log('sending a mail');
res.send(mails.shift());
} else {
console.log('registering a mail listener', nextMailListeners.length);
nextMailListeners.push((mail) => res.send(mail));
}
});
route.get('/routes', (_req, res) => {
res.send(routes);
});
function getShaFromData(data) {
return crypto.createHash('sha512').update(JSON.stringify(data)).digest('hex');
}
function deleteNestedProperty(obj, path) {
try {
const keys = path.split('.');
const lastKey = keys.pop();
const lastObj = keys.reduce((acc, key) => acc && acc[key], obj);
if (lastObj && lastKey in lastObj) {
delete lastObj[lastKey];
}
} catch {
console.log('Attribute not found, with path:', path);
}
}
app.all('*', async (req, res) => {
const call = {
method: req.method,
headers: req.headers,
url: req.url,
body: req.body,
};
if (nextCallListeners.length) {
nextCallListeners.forEach((listener) => listener(call));
nextCallListeners = [];
} else {
calls.push(call);
}
const stringifiedBody = typeof req.body === 'string' ? req.body : JSON.stringify(req.body, null, 2);
for (const route of routes) {
if (
new RegExp(`^${route.request.match}$`).test(req.url) &&
(!route.request.bodyMatch || new RegExp(`^${route.request.bodyMatch}$`, 's').test(stringifiedBody))
) {
console.log(`Call to ${req.url} matched ${route.request.match} ${route.request.bodyMatch || ''}`);
const response = route.response;
res.status(response.status || 200);
res.setHeader('Content-Type', response.contentType || 'application/json');
const body = response.bodies ? response.bodies.shift() : response.body;
res.send(response.contentType ? body : JSON.stringify(body));
if (response.bodies && response.bodies.length === 0) {
routes = routes.filter((r) => r !== nm, route);
}
return;
}
}
const obfuscatedReqBodyForHash = JSON.parse(JSON.stringify(req.body));
recordingsContext.deleteBodyAttributesForHash.forEach((path) => {
deleteNestedProperty(obfuscatedReqBodyForHash, path);
});
const [_, host, ...routeParts] = req.url.split('/');
const headers = {
...req.headers,
...(recordingsContext.forwardHeadersForRoute
// sort longest route first
.sort((a, b) => b.route.length - a.route.length)
.find((forwardHeaders) => req.url.startsWith(forwardHeaders.route))?.headers || {}),
};
const dataToHash = {
url: req.url,
body: obfuscatedReqBodyForHash,
method: req.method,
headers: {
...headers,
// since host can be different based on environment, we need to remove it from the hash
host: '',
},
};
recordingsContext.deleteHeadersForHash.forEach((header) => {
delete dataToHash.headers[header];
});
const hash = getShaFromData(dataToHash);
if (recordingsContext.active) {
try {
const route = `/${routeParts.join('/')}`;
const targetUrl = `https://${host}${route}`;
console.log('Proxying from ', req.url, ' to', targetUrl, ' body: ', req.body);
const proxyRes = await fetch(targetUrl, {
method: req.method,
headers: { ...headers, 'Access-Control-Allow-Origin': '*' },
...(req.method !== 'GET' && req.method !== 'HEAD' && req.body ? { body: JSON.stringify(req.body) } : {}),
});
const status = proxyRes.status;
res.status(proxyRes.status);
const contentType = proxyRes.headers.get('content-type');
let body;
if (contentType && contentType.includes('application/json')) {
body = await proxyRes.json();
res.setHeader('Content-Type', 'application/json');
res.json(body);
} else {
body = await proxyRes.text();
res.send(body);
}
if (!recordingsContext.recordings[hash]) {
recordingsContext.recordings[hash] = [];
}
recordingsContext.recordings[hash].push({
body,
status,
request: dataToHash,
});
} catch (e) {
console.log({
error: e.message + ' ' + req.method,
url: req.url,
});
}
return;
}
const responseFromHash = recordingsContext.recordings[hash]?.shift();
if (responseFromHash) {
const { body, status } = responseFromHash;
res.status(status);
if (typeof body === 'object') {
res.setHeader('Content-Type', 'application/json');
res.send(body);
} else {
try {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.parse(body));
} catch {
res.send(body);
}
}
return;
}
res.status(400).send({
error: {
routes: `Request ${req.url} didn't match any registered route. ${JSON.stringify(req.url, null, 2)}`,
recordings: `Hash ${hash} didn't match any recordings. Request data: ${JSON.stringify(dataToHash, null, 2)}`,
},
url: req.url,
});
});
http.createServer(app).listen(HTTP_PORT, HOST, () => console.log(`Smart mockserver running at ${HOST}:${HTTP_PORT} [HTTP]`));
if (HTTPS_PORT) {
https
.createServer({ key: readFileSync('cert/localhost.key'), cert: readFileSync('cert/localhost.crt') }, app)
.listen(HTTPS_PORT, HOST, () => console.log(`Smart mockserver running at ${HOST}:${HTTPS_PORT} [HTTPS]`));
}