express-mock-server 3.0.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/control.js +2 -2
- package/dist/api/control.js.map +1 -1
- package/dist/api/recording/api-recorder.js +3 -5
- package/dist/api/recording/api-recorder.js.map +1 -1
- package/dist/api/recording/index.js.map +1 -1
- package/dist/api/websocket/index.js +35 -0
- package/dist/api/websocket/index.js.map +1 -0
- package/dist/app.js +11 -0
- package/dist/app.js.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/constants.js +1 -1
- package/dist/constants.js.map +1 -1
- package/dist/core/matchers/MatchItem.js.map +1 -1
- package/dist/core/matchers/MatchingResult.js +1 -1
- package/dist/core/matchers/MatchingResult.js.map +1 -1
- package/dist/core/matchers/http-post-matcher.js.map +1 -1
- package/dist/core/matchers/query-string-matcher.js +21 -4
- package/dist/core/matchers/query-string-matcher.js.map +1 -1
- package/dist/core/request-handler.js +41 -5
- package/dist/core/request-handler.js.map +1 -1
- package/dist/core/sources-parser.js.map +1 -1
- package/dist/core/sources-parser.spec.js.map +1 -1
- package/dist/core/sources-router.js +11 -11
- package/dist/core/sources-router.js.map +1 -1
- package/dist/core/utils.js.map +1 -1
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/lib/api/control.js +5 -5
- package/lib/api/recording/api-recorder.js +6 -6
- package/lib/api/recording/index.js +1 -1
- package/lib/api/websocket/index.js +27 -0
- package/lib/app.js +10 -0
- package/lib/config.js +1 -1
- package/lib/constants.js +1 -1
- package/lib/core/matchers/MatchingResult.js +1 -1
- package/lib/core/matchers/http-post-matcher.js +1 -1
- package/lib/core/matchers/query-string-matcher.js +20 -4
- package/lib/core/request-handler.js +50 -7
- package/lib/core/sources-parser.js +8 -8
- package/lib/core/sources-parser.spec.js +6 -6
- package/lib/core/sources-router.js +12 -12
- package/lib/core/utils.js +1 -1
- package/lib/index.js +33 -1
- package/package.json +17 -15
- package/src/index.js +1 -1
- package/test/image.jpg +0 -0
- package/test/index.js +40 -3
- package/test/mock-api.spec.js +14 -0
- package/test/monitoring-api.spec.js +94 -1
- package/test/responsekey.js +1 -0
- package/test/sources.js +44 -21
- package/test/urls.js +1 -0
- package/test/websocket-api.spec.js +47 -0
|
@@ -34,8 +34,8 @@ class SourcesRouter {
|
|
|
34
34
|
|
|
35
35
|
if (gets) {
|
|
36
36
|
gets.forEach(() => {
|
|
37
|
-
app.get(path, (req, res) => {
|
|
38
|
-
(0, _requestHandler.requestHandler)(this.parser, req, res);
|
|
37
|
+
app.get(path, (req, res, next) => {
|
|
38
|
+
(0, _requestHandler.requestHandler)(this.parser, req, res, next);
|
|
39
39
|
});
|
|
40
40
|
this.logRegisteredPath(isLogEnabled, _constants.methods.GET, path);
|
|
41
41
|
});
|
|
@@ -48,8 +48,8 @@ class SourcesRouter {
|
|
|
48
48
|
|
|
49
49
|
if (posts) {
|
|
50
50
|
posts.forEach(() => {
|
|
51
|
-
app.post(path, (req, res) => {
|
|
52
|
-
(0, _requestHandler.requestHandler)(this.parser, req, res);
|
|
51
|
+
app.post(path, (req, res, next) => {
|
|
52
|
+
(0, _requestHandler.requestHandler)(this.parser, req, res, next);
|
|
53
53
|
});
|
|
54
54
|
this.logRegisteredPath(isLogEnabled, _constants.methods.POST, path);
|
|
55
55
|
});
|
|
@@ -62,8 +62,8 @@ class SourcesRouter {
|
|
|
62
62
|
|
|
63
63
|
if (puts) {
|
|
64
64
|
puts.forEach(() => {
|
|
65
|
-
app.put(path, (req, res) => {
|
|
66
|
-
(0, _requestHandler.requestHandler)(this.parser, req, res);
|
|
65
|
+
app.put(path, (req, res, next) => {
|
|
66
|
+
(0, _requestHandler.requestHandler)(this.parser, req, res, next);
|
|
67
67
|
});
|
|
68
68
|
this.logRegisteredPath(isLogEnabled, _constants.methods.PUT, path);
|
|
69
69
|
});
|
|
@@ -76,8 +76,8 @@ class SourcesRouter {
|
|
|
76
76
|
|
|
77
77
|
if (deletes) {
|
|
78
78
|
deletes.forEach(() => {
|
|
79
|
-
app.delete(path, (req, res) => {
|
|
80
|
-
(0, _requestHandler.requestHandler)(this.parser, req, res);
|
|
79
|
+
app.delete(path, (req, res, next) => {
|
|
80
|
+
(0, _requestHandler.requestHandler)(this.parser, req, res, next);
|
|
81
81
|
});
|
|
82
82
|
this.logRegisteredPath(isLogEnabled, _constants.methods.DELETE, path);
|
|
83
83
|
});
|
|
@@ -90,15 +90,15 @@ class SourcesRouter {
|
|
|
90
90
|
|
|
91
91
|
if (patches) {
|
|
92
92
|
patches.forEach(() => {
|
|
93
|
-
app.patch(path, (req, res) => {
|
|
94
|
-
(0, _requestHandler.requestHandler)(this.parser, req, res);
|
|
93
|
+
app.patch(path, (req, res, next) => {
|
|
94
|
+
(0, _requestHandler.requestHandler)(this.parser, req, res, next);
|
|
95
95
|
});
|
|
96
96
|
this.logRegisteredPath(isLogEnabled, _constants.methods.PATCH, path);
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
* Method to log
|
|
101
|
+
* Method to log registered path
|
|
102
102
|
*/
|
|
103
103
|
|
|
104
104
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../lib/core/sources-router.js"],"names":["SourcesRouter","constructor","parser","registerSources","app","isLogEnabled","map","getMap","Object","keys","forEach","path","registerGET","registerPOST","registerPUT","registerDELETE","registerPATCH","pathItem","gets","methods","GET","get","req","res","logRegisteredPath","posts","POST","post","puts","PUT","put","deletes","DELETE","delete","patches","PATCH","patch","method","console","log","magenta"],"mappings":";;;;;;;AAAA;;AACA;;AAEO,MAAMA,aAAN,CAAoB;AACzB
|
|
1
|
+
{"version":3,"sources":["../../lib/core/sources-router.js"],"names":["SourcesRouter","constructor","parser","registerSources","app","isLogEnabled","map","getMap","Object","keys","forEach","path","registerGET","registerPOST","registerPUT","registerDELETE","registerPATCH","pathItem","gets","methods","GET","get","req","res","next","logRegisteredPath","posts","POST","post","puts","PU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uB3B,IAAvB,EAA6B;AAC5C,QAAIN,YAAJ,EAAkB;AAChBkC,MAAAA,OAAO,CAACC,GAAR,CAAa,OAAMF,MAAO,IAAG3B,IAAK,EAAtB,CAAwB8B,OAApC;AACD;AACF;;AA5FwB","sourcesContent":["import { methods } from '../constants';\nimport { requestHandler } from './request-handler';\n\nexport class SourcesRouter {\n /**\n * @param {SourcesParser} parser\n */\n constructor(parser) {\n this.parser = parser;\n }\n\n registerSources(app, isLogEnabled) {\n const map = this.parser.getMap();\n Object.keys(map).forEach((path) => {\n this.registerGET(app, path, isLogEnabled);\n this.registerPOST(app, path, isLogEnabled);\n this.registerPUT(app, path, isLogEnabled);\n this.registerDELETE(app, path, isLogEnabled);\n this.registerPATCH(app, path, isLogEnabled);\n });\n }\n\n registerGET(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const gets = pathItem[methods.GET];\n\n if (gets) {\n gets.forEach(() => {\n app.get(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.GET, path);\n });\n }\n }\n\n registerPOST(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const posts = pathItem[methods.POST];\n if (posts) {\n posts.forEach(() => {\n app.post(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.POST, path);\n });\n }\n }\n\n registerPUT(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const puts = pathItem[methods.PUT];\n if (puts) {\n puts.forEach(() => {\n app.put(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.PUT, path);\n });\n }\n }\n\n registerDELETE(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const deletes = pathItem[methods.DELETE];\n if (deletes) {\n deletes.forEach(() => {\n app.delete(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.DELETE, path);\n });\n }\n }\n\n registerPATCH(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const patches = pathItem[methods.PATCH];\n if (patches) {\n patches.forEach(() => {\n app.patch(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.PATCH, path);\n });\n }\n }\n\n /**\n * Method to log registered path\n */\n logRegisteredPath(isLogEnabled, method, path) {\n if (isLogEnabled) {\n console.log(`REG ${method} ${path}`.magenta);\n }\n }\n}\n"],"file":"sources-router.js"}
|
package/dist/core/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../lib/core/utils.js"],"names":["isFunction","functionToCheck","getType","toString","call"],"mappings":";;;;;;;AAAO,MAAMA,UAAU,
|
|
1
|
+
{"version":3,"sources":["../../lib/core/utils.js"],"names":["isFunction","functionToCheck","getType","toString","call"],"mappings":";;;;;;;AAAO,MAAMA,UAAU,GAAIC,eAAD,IAAqB;AAC7C,QAAMC,OAAO,GAAG,EAAhB;AACA,SACED,eAAe,IACfC,OAAO,CAACC,QAAR,CAAiBC,IAAjB,CAAsBH,eAAtB,MAA2C,mBAF7C;AAID,CANM","sourcesContent":["export const isFunction = (functionToCheck) => {\n const getType = {};\n return (\n functionToCheck &&\n getType.toString.call(functionToCheck) === '[object Function]'\n );\n};\n"],"file":"utils.js"}
|
package/dist/index.js
CHANGED
|
@@ -12,12 +12,15 @@ var _config = _interopRequireDefault(require("./config"));
|
|
|
12
12
|
|
|
13
13
|
var _app = require("./app");
|
|
14
14
|
|
|
15
|
+
var _ws = _interopRequireDefault(require("ws"));
|
|
16
|
+
|
|
15
17
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
18
|
|
|
17
19
|
const {
|
|
18
20
|
port
|
|
19
21
|
} = _config.default;
|
|
20
|
-
let server
|
|
22
|
+
let server,
|
|
23
|
+
wsServer = null;
|
|
21
24
|
/**
|
|
22
25
|
* Method to start server.
|
|
23
26
|
*
|
|
@@ -25,6 +28,7 @@ let server = null;
|
|
|
25
28
|
* @param {Object} serverConfig
|
|
26
29
|
* @param serverConfig.controlApiUrl ['/api/v1']
|
|
27
30
|
* @param serverConfig.port [8080]
|
|
31
|
+
* @param serverConfig.webSocketEnabled false
|
|
28
32
|
*/
|
|
29
33
|
|
|
30
34
|
function serverStart(sources, serverConfig = {}) {
|
|
@@ -35,6 +39,12 @@ function serverStart(sources, serverConfig = {}) {
|
|
|
35
39
|
console.log(`*************************`.yellow);
|
|
36
40
|
console.log('Server START'.bgGreen.white);
|
|
37
41
|
});
|
|
42
|
+
|
|
43
|
+
if (serverConfig.webSocketEnabled) {
|
|
44
|
+
wsServer = startWebSocket(server);
|
|
45
|
+
app.initWebSocketApi(wsServer);
|
|
46
|
+
}
|
|
47
|
+
|
|
38
48
|
return server;
|
|
39
49
|
}
|
|
40
50
|
/**
|
|
@@ -46,6 +56,30 @@ function serverStop() {
|
|
|
46
56
|
if (server) {
|
|
47
57
|
server.close();
|
|
48
58
|
}
|
|
59
|
+
|
|
60
|
+
if (wsServer) {
|
|
61
|
+
wsServer.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function startWebSocket(server) {
|
|
66
|
+
const wsServer = new _ws.default.Server({
|
|
67
|
+
noServer: true
|
|
68
|
+
});
|
|
69
|
+
wsServer.on('connection', socket => {
|
|
70
|
+
console.log('WebSocket client connected');
|
|
71
|
+
socket.on('message', message => {
|
|
72
|
+
console.log('WebSocket client message received');
|
|
73
|
+
console.log(message);
|
|
74
|
+
});
|
|
75
|
+
socket.on('close', () => console.log('WebSocket client disconnected'));
|
|
76
|
+
});
|
|
77
|
+
server.on('upgrade', (request, socket, head) => {
|
|
78
|
+
wsServer.handleUpgrade(request, socket, head, socket => {
|
|
79
|
+
wsServer.emit('connection', socket, request);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
return wsServer;
|
|
49
83
|
}
|
|
50
84
|
|
|
51
85
|
function getServerPort(serverConfig) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../lib/index.js"],"names":["port","config","server","serverStart","sources","serverConfig","app","App","getServerPort","start","console","log","yellow","bgGreen","white","serverStop","close"],"mappings":";;;;;;;;AAAA;;AACA;;AACA;;;;AAEA,MAAM;AAAEA,EAAAA;AAAF,IAAWC,eAAjB;AACA,IAAIC,
|
|
1
|
+
{"version":3,"sources":["../lib/index.js"],"names":["port","config","server","wsServer","serverStart","sources","serverConfig","app","App","getServerPort","start","console","log","yellow","bgGreen","white","webSocketEnabled","startWebSocket","initWebSocketApi","serverStop","close","ws","Server","noServer","on","socket","message","request","head","handleUpgrade","emit"],"mappings":";;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAEA,MAAM;AAAEA,EAAAA;AAAF,IAAWC,eAAjB;AACA,IAAIC,MAAJ;AAAA,IACEC,QAAQ,GAAG,IADb;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,SAASC,WAAT,CAAqBC,OAArB,EAA8BC,YAAY,GAAG,EAA7C,EAAiD;AACtD,QAAMC,GAAG,GAAG,IAAIC,QAAJ,CAAQH,OAAR,EAAiBC,YAAjB,CAAZ;AACA,QAAMN,IAAI,GAAGS,aAAa,CAACH,YAAD,CAA1B;AACAJ,EAAAA,MAAM,GAAGK,GAAG,CAACG,KAAJ,CAAUV,IAAV,EAAgB,MAAM;AAC7BW,IAAAA,OAAO,CAACC,GAAR,CAAa,oBAAmBZ,IAAK,EAAzB,CAA2Ba,MAAvC;AACAF,IAAAA,OAAO,CAACC,GAAR,CAAa,2BAAD,CAA4BC,MAAxC;AACAF,IAAAA,OAAO,CAACC,GAAR,CAAY,eAAeE,OAAf,CAAuBC,KAAnC;AACD,GAJQ,CAAT;;AAMA,MAAIT,YAAY,CAACU,gBAAjB,EAAmC;AACjCb,IAAAA,QAAQ,GAAGc,cAAc,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AACN,IAAjC,GAAwCA,IAA/C;AACD","sourcesContent":["import 'colors';\nimport config from './config';\nimport { App } from './app';\nimport ws from 'ws';\n\nconst { port } = config;\nlet server,\n wsServer = null;\n\n/**\n * Method to start server.\n *\n * @param sources\n * @param {Object} serverConfig\n * @param serverConfig.controlApiUrl ['/api/v1']\n * @param serverConfig.port [8080]\n * @param serverConfig.webSocketEnabled false\n */\nexport function serverStart(sources, serverConfig = {}) {\n const app = new App(sources, serverConfig);\n const port = getServerPort(serverConfig);\n server = app.start(port, () => {\n console.log(`* Server port ${port}`.yellow);\n console.log(`*************************`.yellow);\n console.log('Server START'.bgGreen.white);\n });\n\n if (serverConfig.webSocketEnabled) {\n wsServer = startWebSocket(server);\n app.initWebSocketApi(wsServer);\n }\n\n return server;\n}\n\n/**\n * Method to stop server.\n */\nexport function serverStop() {\n if (server) {\n server.close();\n }\n if (wsServer) {\n wsServer.close();\n }\n}\n\nfunction startWebSocket(server) {\n const wsServer = new ws.Server({ noServer: true });\n wsServer.on('connection', (socket) => {\n console.log('WebSocket client connected');\n socket.on('message', (message) => {\n console.log('WebSocket client message received');\n console.log(message);\n });\n socket.on('close', () => console.log('WebSocket client disconnected'));\n });\n\n server.on('upgrade', (request, socket, head) => {\n wsServer.handleUpgrade(request, socket, head, (socket) => {\n wsServer.emit('connection', socket, request);\n });\n });\n\n return wsServer;\n}\n\nfunction getServerPort(serverConfig) {\n return serverConfig.port ? serverConfig.port : port;\n}\n"],"file":"index.js"}
|
package/lib/api/control.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import { StatusCodes } from 'http-status-codes';
|
|
3
3
|
import config from '../config';
|
|
4
4
|
|
|
5
5
|
const router = express.Router({});
|
|
6
6
|
const { isLogEnabled } = config;
|
|
7
7
|
|
|
8
|
-
export const controlApi = sourcesParser => {
|
|
8
|
+
export const controlApi = (sourcesParser) => {
|
|
9
9
|
router.post('/set', (req, res) => {
|
|
10
10
|
if (isLogEnabled) {
|
|
11
11
|
console.log('API set new values'.green);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
req.body.forEach(record => {
|
|
14
|
+
req.body.forEach((record) => {
|
|
15
15
|
// console.log('new record is >');
|
|
16
16
|
// console.log(JSON.stringify(record, null, " "));
|
|
17
17
|
// console.log('< new record is');
|
|
18
18
|
sourcesParser.switchRecord(record);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
res.status(ACCEPTED).end();
|
|
21
|
+
res.status(StatusCodes.ACCEPTED).end();
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
router.post('/reset', (req, res) => {
|
|
@@ -28,7 +28,7 @@ export const controlApi = sourcesParser => {
|
|
|
28
28
|
|
|
29
29
|
sourcesParser.resetMap();
|
|
30
30
|
|
|
31
|
-
res.status(ACCEPTED).end();
|
|
31
|
+
res.status(StatusCodes.ACCEPTED).end();
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
return router;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import uuidv1 from 'uuid
|
|
1
|
+
import { v1 as uuidv1 } from 'uuid';
|
|
2
2
|
|
|
3
3
|
const recordingStore = {};
|
|
4
4
|
let isRecordingActive = false;
|
|
@@ -14,21 +14,21 @@ export const startNewRecording = () => {
|
|
|
14
14
|
return uuid;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
export const stopRecording = uuid => {
|
|
17
|
+
export const stopRecording = (uuid) => {
|
|
18
18
|
const recording = recordingStore[uuid];
|
|
19
19
|
delete recordingStore[uuid];
|
|
20
20
|
isRecordingActive = Object.keys(recordingStore).length > 0;
|
|
21
21
|
return recording;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export const apiRecorder = apiUrl => {
|
|
24
|
+
export const apiRecorder = (apiUrl) => {
|
|
25
25
|
return (req, res, next) => {
|
|
26
26
|
if (isRecordingActive && isNotRecordingApiPath(apiUrl, req.path)) {
|
|
27
27
|
const originalSend = res.send;
|
|
28
|
-
res.send = function(body) {
|
|
28
|
+
res.send = function (body) {
|
|
29
29
|
originalSend.call(this, body);
|
|
30
30
|
|
|
31
|
-
Object.keys(recordingStore).forEach(recordUuid => {
|
|
31
|
+
Object.keys(recordingStore).forEach((recordUuid) => {
|
|
32
32
|
recordingStore[recordUuid].push({
|
|
33
33
|
url: req.baseUrl,
|
|
34
34
|
path: req.path,
|
|
@@ -36,7 +36,7 @@ export const apiRecorder = apiUrl => {
|
|
|
36
36
|
params: req.params,
|
|
37
37
|
query: req.query,
|
|
38
38
|
body: req.body,
|
|
39
|
-
responseBody: !body ? {} : JSON.parse(body)
|
|
39
|
+
responseBody: !body ? {} : JSON.parse(body),
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
42
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
|
|
3
|
+
const router = express.Router({});
|
|
4
|
+
|
|
5
|
+
export const websocketApi = (wsServer) => {
|
|
6
|
+
router.post('/websocket/clients/message', (request, response) => {
|
|
7
|
+
wsServer.clients.forEach((ws) => {
|
|
8
|
+
ws.send(JSON.stringify(request.body));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
response.send({
|
|
12
|
+
status: true,
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
router.post('/websocket/clients/disconnect', (request, response) => {
|
|
17
|
+
wsServer.clients.forEach((ws) => {
|
|
18
|
+
ws.close();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
response.send({
|
|
22
|
+
status: true,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return router;
|
|
27
|
+
};
|
package/lib/app.js
CHANGED
|
@@ -7,6 +7,7 @@ import { SourcesParser } from './core/sources-parser';
|
|
|
7
7
|
import { SourcesRouter } from './core/sources-router';
|
|
8
8
|
import { recordingApi } from './api/recording';
|
|
9
9
|
import { apiRecorder } from './api/recording/api-recorder';
|
|
10
|
+
import { websocketApi } from './api/websocket';
|
|
10
11
|
|
|
11
12
|
const { isLogEnabled } = config;
|
|
12
13
|
|
|
@@ -20,10 +21,12 @@ export class App {
|
|
|
20
21
|
|
|
21
22
|
this.initMiddleware();
|
|
22
23
|
this.initControlApi();
|
|
24
|
+
this.initRecordingApi();
|
|
23
25
|
this.initMocks();
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
initMiddleware() {
|
|
29
|
+
this.app.use(bodyParser.text());
|
|
27
30
|
this.app.use(bodyParser.json());
|
|
28
31
|
this.app.use(apiRecorder(this.apiUrl));
|
|
29
32
|
this.initLogger();
|
|
@@ -49,9 +52,16 @@ export class App {
|
|
|
49
52
|
|
|
50
53
|
initControlApi() {
|
|
51
54
|
this.app.use(this.apiUrl, controlApi(this.parser));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
initRecordingApi() {
|
|
52
58
|
this.app.use(this.apiUrl, recordingApi());
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
initWebSocketApi(wsServer) {
|
|
62
|
+
this.app.use(this.apiUrl, websocketApi(wsServer));
|
|
63
|
+
}
|
|
64
|
+
|
|
55
65
|
getApiUrl({ controlApiUrl } = {}) {
|
|
56
66
|
return controlApiUrl ? controlApiUrl : '/api/v1';
|
|
57
67
|
}
|
package/lib/config.js
CHANGED
package/lib/constants.js
CHANGED
|
@@ -52,7 +52,7 @@ export const matchRecordPost = (req, bundle) => {
|
|
|
52
52
|
// let recordBodyKeysLength = Object.keys(recordBodyValue).length;
|
|
53
53
|
// if(requestBodyKeysLength === recordBodyKeysLength){
|
|
54
54
|
let isMatched = true;
|
|
55
|
-
Object.keys(recordBodyValue).forEach(key => {
|
|
55
|
+
Object.keys(recordBodyValue).forEach((key) => {
|
|
56
56
|
let value = recordBodyValue[key];
|
|
57
57
|
if (
|
|
58
58
|
JSON.stringify(value) !== JSON.stringify(requestBody[key])
|
|
@@ -4,11 +4,23 @@ import MatchingResult, {
|
|
|
4
4
|
MATCH_DEFAULT,
|
|
5
5
|
MATCH_NO,
|
|
6
6
|
MATCH_STRICT,
|
|
7
|
-
MATCH_TYPE
|
|
7
|
+
MATCH_TYPE,
|
|
8
8
|
} from './MatchingResult';
|
|
9
9
|
|
|
10
10
|
const TYPE_SPECIFIC_REGEX = 'SPECIFIC_REGEX';
|
|
11
11
|
|
|
12
|
+
const isEqualQueryParamValues = (masterValues, slaveValues) => {
|
|
13
|
+
if (!Array.isArray(masterValues) || !Array.isArray(slaveValues)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (masterValues.length <= 1 || slaveValues.length <= 1) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return [...masterValues].sort().join('') === [...slaveValues].sort().join('');
|
|
22
|
+
};
|
|
23
|
+
|
|
12
24
|
/**
|
|
13
25
|
* exist dont exist dont
|
|
14
26
|
* req/master 0 1 0 1
|
|
@@ -41,7 +53,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
|
41
53
|
if (type === TYPE_SPECIFIC_REGEX) {
|
|
42
54
|
isSpecific = true;
|
|
43
55
|
if (!isContained) {
|
|
44
|
-
values.forEach(value => {
|
|
56
|
+
values.forEach((value) => {
|
|
45
57
|
let regex = new RegExp(value);
|
|
46
58
|
|
|
47
59
|
// console.log(name);
|
|
@@ -56,7 +68,11 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
|
56
68
|
}
|
|
57
69
|
|
|
58
70
|
if (isSpecific && !isContained) {
|
|
59
|
-
|
|
71
|
+
if (isEqualQueryParamValues(masterQS[name], values)) {
|
|
72
|
+
countOfMatches++;
|
|
73
|
+
} else {
|
|
74
|
+
result = MATCH_NO;
|
|
75
|
+
}
|
|
60
76
|
} else {
|
|
61
77
|
if (!isSpecific) countOfMatchesUnspecific++;
|
|
62
78
|
countOfMatches++;
|
|
@@ -80,7 +96,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
|
80
96
|
|
|
81
97
|
export const getMatchesByQueryString = (req, definitions) => {
|
|
82
98
|
let matches = [];
|
|
83
|
-
definitions.forEach(record => {
|
|
99
|
+
definitions.forEach((record) => {
|
|
84
100
|
// console.log(req.query);
|
|
85
101
|
// console.log(record.queryStringParameters);
|
|
86
102
|
let result = getMatchingResultByQueryStrings(
|
|
@@ -1,12 +1,55 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { methods } from '
|
|
1
|
+
import { StatusCodes } from 'http-status-codes';
|
|
2
|
+
import { methods } from '../constants';
|
|
3
3
|
import { isFunction } from './utils';
|
|
4
4
|
import { matchRecordPost } from './matchers/http-post-matcher';
|
|
5
5
|
import { matchRecordGet } from './matchers/http-get-matcher';
|
|
6
6
|
|
|
7
7
|
const APPLICATION_JSON = 'application/json';
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const sendResponse = (response, res, next) => {
|
|
10
|
+
if (response.file) {
|
|
11
|
+
const { type, path } = response.file;
|
|
12
|
+
|
|
13
|
+
if (!type) {
|
|
14
|
+
res
|
|
15
|
+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
|
16
|
+
.send('File type has to be specified!')
|
|
17
|
+
.end();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!path) {
|
|
22
|
+
res
|
|
23
|
+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
|
24
|
+
.send('File path has to be specified!')
|
|
25
|
+
.end();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
res.status(response.statusCode).type(type);
|
|
30
|
+
res.sendFile(
|
|
31
|
+
path,
|
|
32
|
+
{
|
|
33
|
+
dotfiles: 'deny',
|
|
34
|
+
headers: {
|
|
35
|
+
'x-timestamp': Date.now(),
|
|
36
|
+
'x-sent': true,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
function (err) {
|
|
40
|
+
if (err) {
|
|
41
|
+
next(err);
|
|
42
|
+
} else {
|
|
43
|
+
console.log('Sent:', path);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
} else {
|
|
48
|
+
res.send(response.body);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const requestHandler = (sourcesParser, req, res, next) => {
|
|
10
53
|
let method = req.method;
|
|
11
54
|
let path = req.route.path;
|
|
12
55
|
let definitions = sourcesParser.getMap()[path][method];
|
|
@@ -51,7 +94,7 @@ export const requestHandler = (sourcesParser, req, res) => {
|
|
|
51
94
|
console.log('bundle'.yellow);
|
|
52
95
|
// myLog(bundle);
|
|
53
96
|
res
|
|
54
|
-
.status(NOT_FOUND)
|
|
97
|
+
.status(StatusCodes.NOT_FOUND)
|
|
55
98
|
.send('NOT FOUND - no reaponse but why? Look to console.')
|
|
56
99
|
.end();
|
|
57
100
|
return;
|
|
@@ -63,7 +106,7 @@ export const requestHandler = (sourcesParser, req, res) => {
|
|
|
63
106
|
}
|
|
64
107
|
|
|
65
108
|
if (!!response.headers) {
|
|
66
|
-
response.headers.forEach(headerItem => {
|
|
109
|
+
response.headers.forEach((headerItem) => {
|
|
67
110
|
res.setHeader(headerItem['name'], headerItem['values'][0]);
|
|
68
111
|
});
|
|
69
112
|
}
|
|
@@ -72,9 +115,9 @@ export const requestHandler = (sourcesParser, req, res) => {
|
|
|
72
115
|
|
|
73
116
|
if (response.delay) {
|
|
74
117
|
setTimeout(() => {
|
|
75
|
-
|
|
118
|
+
sendResponse(response, res, next);
|
|
76
119
|
}, response.delay);
|
|
77
120
|
} else {
|
|
78
|
-
|
|
121
|
+
sendResponse(response, res, next);
|
|
79
122
|
}
|
|
80
123
|
};
|
|
@@ -47,7 +47,7 @@ export class SourcesParser {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
this.map = {};
|
|
50
|
-
sources.forEach(source => {
|
|
50
|
+
sources.forEach((source) => {
|
|
51
51
|
this.setSource(this.map, source);
|
|
52
52
|
});
|
|
53
53
|
this.sortMap();
|
|
@@ -61,7 +61,7 @@ export class SourcesParser {
|
|
|
61
61
|
*
|
|
62
62
|
*/
|
|
63
63
|
setSource(map, source) {
|
|
64
|
-
source.forEach(record => {
|
|
64
|
+
source.forEach((record) => {
|
|
65
65
|
this.setRecord(map, record);
|
|
66
66
|
});
|
|
67
67
|
}
|
|
@@ -115,7 +115,7 @@ export class SourcesParser {
|
|
|
115
115
|
* Method to sort map
|
|
116
116
|
*/
|
|
117
117
|
sortMap() {
|
|
118
|
-
Object.keys(this.map).forEach(path => {
|
|
118
|
+
Object.keys(this.map).forEach((path) => {
|
|
119
119
|
let pathItem = this.map[path];
|
|
120
120
|
let gets = pathItem[methods.GET];
|
|
121
121
|
if (gets) {
|
|
@@ -152,21 +152,21 @@ function isQueryStringEqual(qs1, qs2) {
|
|
|
152
152
|
let qs1ref = {};
|
|
153
153
|
let qs2ref = {};
|
|
154
154
|
|
|
155
|
-
qs1.forEach(item => {
|
|
155
|
+
qs1.forEach((item) => {
|
|
156
156
|
qs1ref[item['name']] = item['values'];
|
|
157
157
|
});
|
|
158
158
|
|
|
159
|
-
qs2.forEach(item => {
|
|
159
|
+
qs2.forEach((item) => {
|
|
160
160
|
qs2ref[item['name']] = item['values'];
|
|
161
161
|
});
|
|
162
162
|
|
|
163
|
-
Object.keys(qs1ref).forEach(name => {
|
|
163
|
+
Object.keys(qs1ref).forEach((name) => {
|
|
164
164
|
let values = qs1ref[name];
|
|
165
165
|
let qs2Values = qs2ref[name];
|
|
166
166
|
if (qs2Values.length != values.length) {
|
|
167
167
|
isEqual = false;
|
|
168
168
|
} else {
|
|
169
|
-
values.forEach(value => {
|
|
169
|
+
values.forEach((value) => {
|
|
170
170
|
if (qs2Values.indexOf(value) === -1) {
|
|
171
171
|
isEqual = false;
|
|
172
172
|
}
|
|
@@ -192,7 +192,7 @@ function compare(a, b) {
|
|
|
192
192
|
a.queryStringParameters.forEach((qs, index) => {
|
|
193
193
|
if (qs['values'].indexOf(REG_ALL) !== -1) aSpecificCount++;
|
|
194
194
|
});
|
|
195
|
-
b.queryStringParameters.forEach(qs => {
|
|
195
|
+
b.queryStringParameters.forEach((qs) => {
|
|
196
196
|
if (qs['values'].indexOf(REG_ALL) !== -1) bSpecificCount++;
|
|
197
197
|
});
|
|
198
198
|
return aSpecificCount - bSpecificCount;
|
|
@@ -9,16 +9,16 @@ describe('source-parser', () => {
|
|
|
9
9
|
{
|
|
10
10
|
request: {
|
|
11
11
|
method: 'GET',
|
|
12
|
-
path: '/api/test'
|
|
12
|
+
path: '/api/test',
|
|
13
13
|
},
|
|
14
14
|
response: {
|
|
15
15
|
statusCode: 200,
|
|
16
16
|
body: JSON.stringify({
|
|
17
|
-
key: 'value'
|
|
18
|
-
})
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
]
|
|
17
|
+
key: 'value',
|
|
18
|
+
}),
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
22
|
];
|
|
23
23
|
|
|
24
24
|
// when
|