forked from jdhornsby/terraunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock-aws-api.js
37 lines (32 loc) · 1.36 KB
/
mock-aws-api.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
const express = require('express');
const bodyParser = require('body-parser');
const defaultMocks = require('./data/default-mocks.json');
const {isDebugModeOn} = require('./utils');
function Mock(options = {}) {
this.port = options.port || 9999;
this.mockAwsResponses = options.mockAwsResponses || [];
this.debugMode = options.debugMode || DEBUG_MODE.LOCAL;
this.app = express();
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.route('*').all((req, res) => {
const mockId = req.path.replace(/\//g, '') + ':' + (req.body && req.body.Action ? req.body.Action : '');
const mockResponse =
this.mockAwsResponses.find(m => m.id == mockId) ||
defaultMocks.find(m => m.id == mockId) ||
this.mockAwsResponses.find(m => m.id == 'default') ||
defaultMocks.find(m => m.id == 'default');
if(isDebugModeOn(this.debugMode)) {
console.log('Mock API received action ' + mockId + ', used mockResponse ' + mockResponse.id);;
}
res.statusCode = mockResponse.statusCode;
res.setHeader('Content-Type', 'application/xml');
res.send(mockResponse.body);
});
this.start = () => {
this.server = this.app.listen(this.port);
};
this.stop = async () => {
await this.server.close();
};
};
module.exports = Mock;