express-mock-server 3.4.3 → 3.5.1
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/README.md +5 -0
- package/dist/api/recording/api-recorder.js +12 -1
- package/dist/api/recording/api-recorder.js.map +1 -1
- package/dist/config.js +24 -22
- package/dist/config.js.map +1 -1
- package/package.json +4 -2
- package/.editorconfig +0 -13
- package/.prettierrc +0 -4
- package/dist/core/sources-parser.spec.js +0 -32
- package/dist/core/sources-parser.spec.js.map +0 -1
- package/lib/api/control.js +0 -35
- package/lib/api/recording/api-recorder.js +0 -47
- package/lib/api/recording/index.js +0 -23
- package/lib/api/websocket/index.js +0 -27
- package/lib/app.js +0 -76
- package/lib/config.js +0 -29
- package/lib/constants.js +0 -9
- package/lib/core/matchers/MatchItem.js +0 -11
- package/lib/core/matchers/MatchingResult.js +0 -80
- package/lib/core/matchers/http-get-matcher.js +0 -12
- package/lib/core/matchers/http-post-matcher.js +0 -124
- package/lib/core/matchers/query-string-matcher.js +0 -113
- package/lib/core/request-handler.js +0 -129
- package/lib/core/sources-parser.js +0 -200
- package/lib/core/sources-parser.spec.js +0 -31
- package/lib/core/sources-router.js +0 -97
- package/lib/core/utils.js +0 -7
- package/lib/index.js +0 -70
- package/src/index.js +0 -4
- package/test/Digger.js +0 -20
- package/test/control-api.spec.js +0 -105
- package/test/image.jpg +0 -0
- package/test/index.js +0 -183
- package/test/mock-api.spec.js +0 -46
- package/test/monitoring-api.spec.js +0 -236
- package/test/responsekey.js +0 -15
- package/test/sources.js +0 -174
- package/test/urls.js +0 -14
- package/test/websocket-api.spec.js +0 -47
package/README.md
CHANGED
|
@@ -61,3 +61,8 @@ var opt_serverConfig = {
|
|
|
61
61
|
serverStart(sources [, opt_serverConfig])
|
|
62
62
|
|
|
63
63
|
```
|
|
64
|
+
|
|
65
|
+
## Advanced configuration
|
|
66
|
+
The following environment variables can be set
|
|
67
|
+
- `LOGGING`: If set to 'ON', enables request logging in the console.
|
|
68
|
+
- `PORT`: Optionally sets the default port to use when it is not explicitly passed to `serverStart(...)`.
|
|
@@ -14,6 +14,17 @@ const isNotRecordingApiPath = (apiUrl, path) => {
|
|
|
14
14
|
return path.indexOf(`${apiUrl}/recording/`) === -1;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
const parseResponseBody = body => {
|
|
18
|
+
if (!body) return {};
|
|
19
|
+
if (typeof body !== 'string') return body;
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(body);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return body;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
17
28
|
const startNewRecording = () => {
|
|
18
29
|
const uuid = (0, _uuid.v1)();
|
|
19
30
|
recordingStore[uuid] = [];
|
|
@@ -47,7 +58,7 @@ const apiRecorder = apiUrl => {
|
|
|
47
58
|
params: req.params,
|
|
48
59
|
query: req.query,
|
|
49
60
|
body: req.body,
|
|
50
|
-
responseBody:
|
|
61
|
+
responseBody: parseResponseBody(body)
|
|
51
62
|
});
|
|
52
63
|
});
|
|
53
64
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/api/recording/api-recorder.js"],"names":["recordingStore","isRecordingActive","isNotRecordingApiPath","apiUrl","path","indexOf","startNewRecording","uuid","stopRecording","recording","Object","keys","length","apiRecorder","req","res","next","originalSend","send","
|
|
1
|
+
{"version":3,"sources":["../../../lib/api/recording/api-recorder.js"],"names":["recordingStore","isRecordingActive","isNotRecordingApiPath","apiUrl","path","indexOf","parseResponseBody","body","JSON","parse","e","startNewRecording","uuid","stopRecording","recording","Object","keys","length","apiRecorder","req","res","next","originalSend","send","call","forEach","recordUuid","push","url","baseUrl","method","params","query","responseBody"],"mappings":";;;;;;;AAAA;;AAEA,MAAMA,cAAc,GAAG,EAAvB;AACA,IAAIC,iBAAiB,GAAG,KAAxB;;AAEA,MAAMC,qBAAqB,GAAG,CAACC,MAAD,EAASC,IAAT,KAAkB;AAC9C,SAAOA,IAAI,CAACC,OAAL,CAAc,GAAEF,MAAO,aAAvB,MAAyC,CAAC,CAAjD;AACD,CAFD;;AAIA,MAAMG,iBAAiB,GAAIC,IAAD,IAAU;AAClC,MAAI,CAACA,IAAL,EAAW,OAAO,EAAP;AACX,MAAI,OAAOA,IAAP,KAAgB,QAApB,EAA8B,OAAOA,IAAP;;AAE9B,MAAI;AACF,WAAOC,IAAI,CAACC,KAAL,CAAWF,IAAX,CAAP;AACD,GAFD,CAEE,OAAOG,CAAP,EAAU;AACV,WAAOH,IAAP;AACD;AACF,CATD;;AAWO,MAAMI,iBAAiB,GAAG,MAAM;AACrC,QAAMC,IAAI,GAAG,eAAb;AACAZ,EAAAA,cAAc,CAACY,IAAD,CAAd,GAAuB,EAAvB;AACAX,EAAAA,iBAAiB,GAAG,IAApB;AACA,SAAOW,IAAP;AACD,CALM;;;;AAOA,MAAMC,aAAa,GAAID,IAAD,IAAU;AACrC,QAAME,SAAS,GAAGd,cAAc,CAACY,IAAD,CAAhC;AACA,SAAOZ,cAAc,CAACY,IAAD,CAArB;AACAX,EAAAA,iBAAiB,GAAGc,MAAM,CAACC,IAAP,CAAYhB,cAAZ,EAA4BiB,MAA5B,GAAqC,CAAzD;AACA,SAAOH,SAAP;AACD,CALM;;;;AAOA,MAAMI,WAAW,GAAIf,MAAD,IAAY;AACrC,SAAO,CAACgB,GAAD,EAAMC,GAAN,EAAWC,IAAX,KAAoB;AACzB,QAAIpB,iBAAiB,IAAIC,qBAAqB,CAACC,MAAD,EAASgB,GAAG,CAACf,IAAb,CAA9C,EAAkE;AAChE,YAAMkB,YAAY,GAAGF,GAAG,CAACG,IAAzB;;AACAH,MAAAA,GAAG,CAACG,IAAJ,GAAW,UAAUhB,IAAV,EAAgB;AACzBe,QAAAA,YAAY,CAACE,IAAb,CAAkB,IAAlB,EAAwBjB,IAAxB;AAEAQ,QAAAA,MAAM,CAACC,IAAP,CAAYhB,cAAZ,EAA4ByB,OAA5B,CAAqCC,UAAD,IAAgB;AAClD1B,UAAAA,cAAc,CAAC0B,UAAD,CAAd,CAA2BC,IAA3B,CAAgC;AAC9BC,YAAAA,GAAG,EAAET,GAAG,CAACU,OADqB;AAE9BzB,YAAAA,IAAI,EAAEe,GAAG,CAACf,IAFoB;AAG9B0B,YAAAA,MAAM,EAAEX,GAAG,CAACW,MAHkB;AAI9BC,YAAAA,MAAM,EAAEZ,GAAG,CAACY,MAJkB;AAK9BC,YAAAA,KAAK,EAAEb,GAAG,CAACa,KALmB;AAM9BzB,YAAAA,IAAI,EAAEY,GAAG,CAACZ,IANoB;AAO9B0B,YAAAA,YAAY,EAAE3B,iBAAiB,CAACC,IAAD;AAPD,WAAhC;AASD,SAVD;AAWD,OAdD;AAeD;;AAEDc,IAAAA,IAAI;AACL,GArBD;AAsBD,CAvBM","sourcesContent":["import { v1 as uuidv1 } from 'uuid';\n\nconst recordingStore = {};\nlet isRecordingActive = false;\n\nconst isNotRecordingApiPath = (apiUrl, path) => {\n return path.indexOf(`${apiUrl}/recording/`) === -1;\n};\n\nconst parseResponseBody = (body) => {\n if (!body) return {};\n if (typeof body !== 'string') return body;\n\n try {\n return JSON.parse(body);\n } catch (e) {\n return body;\n }\n};\n\nexport const startNewRecording = () => {\n const uuid = uuidv1();\n recordingStore[uuid] = [];\n isRecordingActive = true;\n return uuid;\n};\n\nexport const stopRecording = (uuid) => {\n const recording = recordingStore[uuid];\n delete recordingStore[uuid];\n isRecordingActive = Object.keys(recordingStore).length > 0;\n return recording;\n};\n\nexport const apiRecorder = (apiUrl) => {\n return (req, res, next) => {\n if (isRecordingActive && isNotRecordingApiPath(apiUrl, req.path)) {\n const originalSend = res.send;\n res.send = function (body) {\n originalSend.call(this, body);\n\n Object.keys(recordingStore).forEach((recordUuid) => {\n recordingStore[recordUuid].push({\n url: req.baseUrl,\n path: req.path,\n method: req.method,\n params: req.params,\n query: req.query,\n body: req.body,\n responseBody: parseResponseBody(body),\n });\n });\n };\n }\n\n next();\n };\n};\n"],"file":"api-recorder.js"}
|
package/dist/config.js
CHANGED
|
@@ -4,26 +4,27 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _nconf = _interopRequireDefault(require("nconf"));
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
|
-
// http://www.codedependant.net/2015/01/31/production-ready-node-configuration
|
|
13
|
-
// import { execSync } from 'child_process';
|
|
14
7
|
// Specifying an env delimiter allows us to override config when shipping to
|
|
15
|
-
// production server. 'foo__bar=2
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
// production server. 'foo__bar=2 npm start' will set config to '{foo: {bar: 2}}'
|
|
9
|
+
const ENV_DELIMITER = '__';
|
|
10
|
+
|
|
11
|
+
const expandEnv = env => Object.keys(env).reduce((result, key) => {
|
|
12
|
+
const path = key.split(ENV_DELIMITER);
|
|
13
|
+
const leaf = path.pop();
|
|
14
|
+
const parent = path.reduce((node, segment) => {
|
|
15
|
+
if (typeof node[segment] !== 'object' || node[segment] === null) {
|
|
16
|
+
node[segment] = {};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return node[segment];
|
|
20
|
+
}, result);
|
|
21
|
+
parent[leaf] = env[key];
|
|
22
|
+
return result;
|
|
23
|
+
}, {}); // Remember, never put secrets in default config.
|
|
24
|
+
// Use environment variables for production.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const defaults = {
|
|
27
28
|
appName: 'node mock server',
|
|
28
29
|
appVersion: 1,
|
|
29
30
|
defaultLocale: 'en',
|
|
@@ -31,9 +32,10 @@ _nconf.default.defaults({
|
|
|
31
32
|
isLogEnabled: process.env.LOGGING === 'ON',
|
|
32
33
|
locales: ['cs', 'en'],
|
|
33
34
|
port: process.env.PORT || 8080
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var _default = _nconf.default.get();
|
|
35
|
+
}; // Environment variables take precedence over the defaults.
|
|
37
36
|
|
|
37
|
+
var _default = { ...defaults,
|
|
38
|
+
...expandEnv(process.env)
|
|
39
|
+
};
|
|
38
40
|
exports.default = _default;
|
|
39
41
|
//# sourceMappingURL=config.js.map
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../lib/config.js"],"names":["
|
|
1
|
+
{"version":3,"sources":["../lib/config.js"],"names":["ENV_DELIMITER","expandEnv","env","Object","keys","reduce","result","key","path","split","leaf","pop","parent","node","segment","defaults","appName","appVersion","defaultLocale","isProduction","process","NODE_ENV","isLogEnabled","LOGGING","locales","port","PORT"],"mappings":";;;;;;AAAA;AACA;AACA,MAAMA,aAAa,GAAG,IAAtB;;AAEA,MAAMC,SAAS,GAAIC,GAAD,IAChBC,MAAM,CAACC,IAAP,CAAYF,GAAZ,EAAiBG,MAAjB,CAAwB,CAACC,MAAD,EAASC,GAAT,KAAiB;AACvC,QAAMC,IAAI,GAAGD,GAAG,CAACE,KAAJ,CAAUT,aAAV,CAAb;AACA,QAAMU,IAAI,GAAGF,IAAI,CAACG,GAAL,EAAb;AAEA,QAAMC,MAAM,GAAGJ,IAAI,CAACH,MAAL,CAAY,CAACQ,IAAD,EAAOC,OAAP,KAAmB;AAC5C,QAAI,OAAOD,IAAI,CAACC,OAAD,CAAX,KAAyB,QAAzB,IAAqCD,IAAI,CAACC,OAAD,CAAJ,KAAkB,IAA3D,EAAiE;AAC/DD,MAAAA,IAAI,CAACC,OAAD,CAAJ,GAAgB,EAAhB;AACD;;AAED,WAAOD,IAAI,CAACC,OAAD,CAAX;AACD,GANc,EAMZR,MANY,CAAf;AAQAM,EAAAA,MAAM,CAACF,IAAD,CAAN,GAAeR,GAAG,CAACK,GAAD,CAAlB;AAEA,SAAOD,MAAP;AACD,CAfD,EAeG,EAfH,CADF,C,CAkBA;AACA;;;AACA,MAAMS,QAAQ,GAAG;AACfC,EAAAA,OAAO,EAAE,kBADM;AAEfC,EAAAA,UAAU,EAAE,CAFG;AAGfC,EAAAA,aAAa,EAAE,IAHA;AAIfC,EAAAA,YAAY,EAAEC,OAAO,CAAClB,GAAR,CAAYmB,QAAZ,KAAyB,YAJxB;AAKfC,EAAAA,YAAY,EAAEF,OAAO,CAAClB,GAAR,CAAYqB,OAAZ,KAAwB,IALvB;AAMfC,EAAAA,OAAO,EAAE,CAAC,IAAD,EAAO,IAAP,CANM;AAOfC,EAAAA,IAAI,EAAEL,OAAO,CAAClB,GAAR,CAAYwB,IAAZ,IAAoB;AAPX,CAAjB,C,CAUA;;eACe,EAAE,GAAGX,QAAL;AAAe,KAAGd,SAAS,CAACmB,OAAO,CAAClB,GAAT;AAA3B,C","sourcesContent":["// Specifying an env delimiter allows us to override config when shipping to\n// production server. 'foo__bar=2 npm start' will set config to '{foo: {bar: 2}}'\nconst ENV_DELIMITER = '__';\n\nconst expandEnv = (env) =>\n Object.keys(env).reduce((result, key) => {\n const path = key.split(ENV_DELIMITER);\n const leaf = path.pop();\n\n const parent = path.reduce((node, segment) => {\n if (typeof node[segment] !== 'object' || node[segment] === null) {\n node[segment] = {};\n }\n\n return node[segment];\n }, result);\n\n parent[leaf] = env[key];\n\n return result;\n }, {});\n\n// Remember, never put secrets in default config.\n// Use environment variables for production.\nconst defaults = {\n appName: 'node mock server',\n appVersion: 1,\n defaultLocale: 'en',\n isProduction: process.env.NODE_ENV === 'production',\n isLogEnabled: process.env.LOGGING === 'ON',\n locales: ['cs', 'en'],\n port: process.env.PORT || 8080,\n};\n\n// Environment variables take precedence over the defaults.\nexport default { ...defaults, ...expandEnv(process.env) };"],"file":"config.js"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-mock-server",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.1",
|
|
4
4
|
"description": "Mock server powered by Express.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
6
9
|
"scripts": {
|
|
7
10
|
"prepublishOnly": "npm run build",
|
|
8
11
|
"prepare": "npm run build",
|
|
@@ -33,7 +36,6 @@
|
|
|
33
36
|
"express": "4.17.1",
|
|
34
37
|
"http-status-codes": "2.1.4",
|
|
35
38
|
"morgan": "1.10.0",
|
|
36
|
-
"nconf": "0.11.3",
|
|
37
39
|
"rimraf": "3.0.2",
|
|
38
40
|
"uuid": "8.3.2",
|
|
39
41
|
"ws": "7.4.6"
|
package/.editorconfig
DELETED
package/.prettierrc
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _sourcesParser = require("./sources-parser");
|
|
4
|
-
|
|
5
|
-
var _assert = _interopRequireDefault(require("assert"));
|
|
6
|
-
|
|
7
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
-
|
|
9
|
-
describe('source-parser', () => {
|
|
10
|
-
it('should parse source', () => {
|
|
11
|
-
// given
|
|
12
|
-
const sources = [[{
|
|
13
|
-
request: {
|
|
14
|
-
method: 'GET',
|
|
15
|
-
path: '/api/test'
|
|
16
|
-
},
|
|
17
|
-
response: {
|
|
18
|
-
statusCode: 200,
|
|
19
|
-
body: JSON.stringify({
|
|
20
|
-
key: 'value'
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
}]]; // when
|
|
24
|
-
|
|
25
|
-
const parser = new _sourcesParser.SourcesParser(sources); // then
|
|
26
|
-
|
|
27
|
-
_assert.default.equal(Object.keys(parser.map).length, 1);
|
|
28
|
-
|
|
29
|
-
_assert.default.equal(Object.keys(parser.map)[0], '/api/test');
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
//# sourceMappingURL=sources-parser.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../lib/core/sources-parser.spec.js"],"names":["describe","it","sources","request","method","path","response","statusCode","body","JSON","stringify","key","parser","SourcesParser","assert","equal","Object","keys","map","length"],"mappings":";;AAAA;;AACA;;;;AAEAA,QAAQ,CAAC,eAAD,EAAkB,MAAM;AAC9BC,EAAAA,EAAE,CAAC,qBAAD,EAAwB,MAAM;AAC9B;AACA,UAAMC,OAAO,GAAG,CACd,CACE;AACEC,MAAAA,OAAO,EAAE;AACPC,QAAAA,MAAM,EAAE,KADD;AAEPC,QAAAA,IAAI,EAAE;AAFC,OADX;AAKEC,MAAAA,QAAQ,EAAE;AACRC,QAAAA,UAAU,EAAE,GADJ;AAERC,QAAAA,IAAI,EAAEC,IAAI,CAACC,SAAL,CAAe;AACnBC,UAAAA,GAAG,EAAE;AADc,SAAf;AAFE;AALZ,KADF,CADc,CAAhB,CAF8B,CAmB9B;;AACA,UAAMC,MAAM,GAAG,IAAIC,4BAAJ,CAAkBX,OAAlB,CAAf,CApB8B,CAsB9B;;AACAY,oBAAOC,KAAP,CAAaC,MAAM,CAACC,IAAP,CAAYL,MAAM,CAACM,GAAnB,EAAwBC,MAArC,EAA6C,CAA7C;;AACAL,oBAAOC,KAAP,CAAaC,MAAM,CAACC,IAAP,CAAYL,MAAM,CAACM,GAAnB,EAAwB,CAAxB,CAAb,EAAyC,WAAzC;AACD,GAzBC,CAAF;AA0BD,CA3BO,CAAR","sourcesContent":["import { SourcesParser } from './sources-parser';\nimport assert from 'assert';\n\ndescribe('source-parser', () => {\n it('should parse source', () => {\n // given\n const sources = [\n [\n {\n request: {\n method: 'GET',\n path: '/api/test',\n },\n response: {\n statusCode: 200,\n body: JSON.stringify({\n key: 'value',\n }),\n },\n },\n ],\n ];\n\n // when\n const parser = new SourcesParser(sources);\n\n // then\n assert.equal(Object.keys(parser.map).length, 1);\n assert.equal(Object.keys(parser.map)[0], '/api/test');\n });\n});\n"],"file":"sources-parser.spec.js"}
|
package/lib/api/control.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { StatusCodes } from 'http-status-codes';
|
|
3
|
-
import config from '../config';
|
|
4
|
-
|
|
5
|
-
const router = express.Router({});
|
|
6
|
-
const { isLogEnabled } = config;
|
|
7
|
-
|
|
8
|
-
export const controlApi = (sourcesParser) => {
|
|
9
|
-
router.post('/set', (req, res) => {
|
|
10
|
-
if (isLogEnabled) {
|
|
11
|
-
console.log('API set new values'.green);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
req.body.forEach((record) => {
|
|
15
|
-
// console.log('new record is >');
|
|
16
|
-
// console.log(JSON.stringify(record, null, " "));
|
|
17
|
-
// console.log('< new record is');
|
|
18
|
-
sourcesParser.switchRecord(record);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
res.status(StatusCodes.ACCEPTED).end();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
router.post('/reset', (req, res) => {
|
|
25
|
-
if (isLogEnabled) {
|
|
26
|
-
console.log('API Reset'.green);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
sourcesParser.resetMap();
|
|
30
|
-
|
|
31
|
-
res.status(StatusCodes.ACCEPTED).end();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return router;
|
|
35
|
-
};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { v1 as uuidv1 } from 'uuid';
|
|
2
|
-
|
|
3
|
-
const recordingStore = {};
|
|
4
|
-
let isRecordingActive = false;
|
|
5
|
-
|
|
6
|
-
const isNotRecordingApiPath = (apiUrl, path) => {
|
|
7
|
-
return path.indexOf(`${apiUrl}/recording/`) === -1;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const startNewRecording = () => {
|
|
11
|
-
const uuid = uuidv1();
|
|
12
|
-
recordingStore[uuid] = [];
|
|
13
|
-
isRecordingActive = true;
|
|
14
|
-
return uuid;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const stopRecording = (uuid) => {
|
|
18
|
-
const recording = recordingStore[uuid];
|
|
19
|
-
delete recordingStore[uuid];
|
|
20
|
-
isRecordingActive = Object.keys(recordingStore).length > 0;
|
|
21
|
-
return recording;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const apiRecorder = (apiUrl) => {
|
|
25
|
-
return (req, res, next) => {
|
|
26
|
-
if (isRecordingActive && isNotRecordingApiPath(apiUrl, req.path)) {
|
|
27
|
-
const originalSend = res.send;
|
|
28
|
-
res.send = function (body) {
|
|
29
|
-
originalSend.call(this, body);
|
|
30
|
-
|
|
31
|
-
Object.keys(recordingStore).forEach((recordUuid) => {
|
|
32
|
-
recordingStore[recordUuid].push({
|
|
33
|
-
url: req.baseUrl,
|
|
34
|
-
path: req.path,
|
|
35
|
-
method: req.method,
|
|
36
|
-
params: req.params,
|
|
37
|
-
query: req.query,
|
|
38
|
-
body: req.body,
|
|
39
|
-
responseBody: !body ? {} : JSON.parse(body),
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
next();
|
|
46
|
-
};
|
|
47
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { startNewRecording, stopRecording } from './api-recorder';
|
|
3
|
-
|
|
4
|
-
const router = express.Router({});
|
|
5
|
-
|
|
6
|
-
export const recordingApi = () => {
|
|
7
|
-
router.post('/recording/start', (req, res) => {
|
|
8
|
-
const uuid = startNewRecording();
|
|
9
|
-
res.send({
|
|
10
|
-
id: uuid,
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
router.post('/recording/:id/stop', (req, res) => {
|
|
15
|
-
const recording = stopRecording(req.params.id);
|
|
16
|
-
if (!recording) {
|
|
17
|
-
res.status(404).send('The monitoring with given ID was not found.');
|
|
18
|
-
}
|
|
19
|
-
res.send(recording);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return router;
|
|
23
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import morgan from 'morgan';
|
|
3
|
-
import bodyParser from 'body-parser';
|
|
4
|
-
import config from './config';
|
|
5
|
-
import { controlApi } from './api/control';
|
|
6
|
-
import { SourcesParser } from './core/sources-parser';
|
|
7
|
-
import { SourcesRouter } from './core/sources-router';
|
|
8
|
-
import { recordingApi } from './api/recording';
|
|
9
|
-
import { apiRecorder } from './api/recording/api-recorder';
|
|
10
|
-
import { websocketApi } from './api/websocket';
|
|
11
|
-
|
|
12
|
-
const { isLogEnabled } = config;
|
|
13
|
-
|
|
14
|
-
export class App {
|
|
15
|
-
constructor(sources, serverConfig, customMiddlewareFn) {
|
|
16
|
-
this.parser = new SourcesParser(sources);
|
|
17
|
-
this.apiUrl = this.getApiUrl(serverConfig);
|
|
18
|
-
this.app = express();
|
|
19
|
-
|
|
20
|
-
console.log('******* CONFIG **********'.yellow);
|
|
21
|
-
|
|
22
|
-
this.initMiddleware(customMiddlewareFn);
|
|
23
|
-
this.initControlApi();
|
|
24
|
-
this.initRecordingApi();
|
|
25
|
-
this.initMocks();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
initMiddleware(customMiddlewareFn) {
|
|
29
|
-
this.app.use(bodyParser.text());
|
|
30
|
-
this.app.use(bodyParser.json());
|
|
31
|
-
this.app.use(apiRecorder(this.apiUrl));
|
|
32
|
-
this.initLogger();
|
|
33
|
-
|
|
34
|
-
if (customMiddlewareFn) {
|
|
35
|
-
customMiddlewareFn(this.app.use.bind(this.app));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
initMocks() {
|
|
40
|
-
const router = new SourcesRouter(this.parser);
|
|
41
|
-
router.registerSources(this.app, isLogEnabled);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Method to start logger of requests
|
|
46
|
-
* Actual format
|
|
47
|
-
* 0.230 ms GET 200 /some/url/
|
|
48
|
-
* More option
|
|
49
|
-
* https://github.com/expressjs/morgan
|
|
50
|
-
*/
|
|
51
|
-
initLogger() {
|
|
52
|
-
if (isLogEnabled) {
|
|
53
|
-
this.app.use(morgan(':response-time ms :method :status :url'));
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
initControlApi() {
|
|
58
|
-
this.app.use(this.apiUrl, controlApi(this.parser));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
initRecordingApi() {
|
|
62
|
-
this.app.use(this.apiUrl, recordingApi());
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
initWebSocketApi(wsServer) {
|
|
66
|
-
this.app.use(this.apiUrl, websocketApi(wsServer));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
getApiUrl({ controlApiUrl } = {}) {
|
|
70
|
-
return controlApiUrl ? controlApiUrl : '/api/v1';
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
start(port, callback) {
|
|
74
|
-
return this.app.listen(port, callback);
|
|
75
|
-
}
|
|
76
|
-
}
|
package/lib/config.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// http://www.codedependant.net/2015/01/31/production-ready-node-configuration
|
|
2
|
-
import nconf from 'nconf';
|
|
3
|
-
// import { execSync } from 'child_process';
|
|
4
|
-
|
|
5
|
-
// Specifying an env delimiter allows us to override config when shipping to
|
|
6
|
-
// production server. 'foo__bar=2 gulp' will set config to '{foo: {bar: 2}}'
|
|
7
|
-
nconf.env('__');
|
|
8
|
-
|
|
9
|
-
// For local development with secrets. Check src/common/_secrets.json file.
|
|
10
|
-
// nconf.file('src/common/secrets.json');
|
|
11
|
-
|
|
12
|
-
// // The semver is for libraries, apps are versioned by git commit SHA.
|
|
13
|
-
// const sourceVersion = process.env.SOURCE_VERSION || execSync('git rev-parse HEAD')
|
|
14
|
-
// .toString()
|
|
15
|
-
// .trim();
|
|
16
|
-
|
|
17
|
-
// Remember, never put secrets in default config.
|
|
18
|
-
// Use environment variables for production, and secrets.json for development.
|
|
19
|
-
nconf.defaults({
|
|
20
|
-
appName: 'node mock server',
|
|
21
|
-
appVersion: 1,
|
|
22
|
-
defaultLocale: 'en',
|
|
23
|
-
isProduction: process.env.NODE_ENV === 'production',
|
|
24
|
-
isLogEnabled: process.env.LOGGING === 'ON',
|
|
25
|
-
locales: ['cs', 'en'],
|
|
26
|
-
port: process.env.PORT || 8080,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
export default nconf.get();
|
package/lib/constants.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
export const MATCH_TYPE = {
|
|
2
|
-
STRICT: 'strict',
|
|
3
|
-
SUBSET: 'subset', // some QS match
|
|
4
|
-
DEFAULT: 'default', // request have QS but bundle doesnt
|
|
5
|
-
NO: 'no',
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
class MatchingResult {
|
|
12
|
-
constructor(type, countOfMatches = null, countOfMatchesUnspecific = null) {
|
|
13
|
-
this.type = type;
|
|
14
|
-
this.count = countOfMatches;
|
|
15
|
-
this.countUnspecific = countOfMatchesUnspecific;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
isDefault() {
|
|
19
|
-
return this.type === MATCH_TYPE.DEFAULT;
|
|
20
|
-
}
|
|
21
|
-
isStrict() {
|
|
22
|
-
return this.type === MATCH_TYPE.STRICT;
|
|
23
|
-
}
|
|
24
|
-
isNo() {
|
|
25
|
-
return this.type === MATCH_TYPE.NO;
|
|
26
|
-
}
|
|
27
|
-
isSubset() {
|
|
28
|
-
return this.type === MATCH_TYPE.SUBSET;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* STRICT SUBSET DEFAULT NO
|
|
33
|
-
* STRICT 0 1 1 1
|
|
34
|
-
* SUBSET -1 c 1 1
|
|
35
|
-
* DEFAULT -1 -1 0 1
|
|
36
|
-
* NO -1 -1 -1 0
|
|
37
|
-
* @param {MatchingResult} other
|
|
38
|
-
* @return {number}
|
|
39
|
-
*/
|
|
40
|
-
compare(other) {
|
|
41
|
-
if (other.type === this.type) {
|
|
42
|
-
if (this.isSubset()) {
|
|
43
|
-
if (this.count === other.count) {
|
|
44
|
-
return this.countUnspecific - other.countUnspecific;
|
|
45
|
-
} else {
|
|
46
|
-
return this.count - other.count;
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
return 0;
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
if (this.isStrict()) return 1;
|
|
53
|
-
if (other.isStrict()) return -1;
|
|
54
|
-
if (this.isNo()) return -1;
|
|
55
|
-
if (other.isNo()) return 1;
|
|
56
|
-
if (this.isSubset() && other.isDefault()) return 1;
|
|
57
|
-
if (this.isDefault() && other.isSubset()) return -1;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Just develop tool
|
|
63
|
-
*/
|
|
64
|
-
print() {
|
|
65
|
-
let toPrint = 'MatchingResult type: ' + this.type;
|
|
66
|
-
if (this.count !== null) {
|
|
67
|
-
toPrint += ', counts: ' + this.count;
|
|
68
|
-
}
|
|
69
|
-
if (this.countUnspecific !== null) {
|
|
70
|
-
toPrint += ', countUnspecific: ' + this.countUnspecific;
|
|
71
|
-
}
|
|
72
|
-
console.log(toPrint.bgBlue.white);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export const MATCH_NO = new MatchingResult(MATCH_TYPE.NO);
|
|
77
|
-
export const MATCH_STRICT = new MatchingResult(MATCH_TYPE.STRICT);
|
|
78
|
-
export const MATCH_DEFAULT = new MatchingResult(MATCH_TYPE.DEFAULT);
|
|
79
|
-
|
|
80
|
-
export default MatchingResult;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { getMatchesByQueryString } from './query-string-matcher';
|
|
2
|
-
|
|
3
|
-
export const matchRecordGet = (req, definitions) => {
|
|
4
|
-
let matches = getMatchesByQueryString(req, definitions);
|
|
5
|
-
matches.sort((a, b) => {
|
|
6
|
-
return b.result.compare(a.result);
|
|
7
|
-
});
|
|
8
|
-
// console.log('+++++++++++++++++');
|
|
9
|
-
// myLog(matches);
|
|
10
|
-
if (matches.length === 0) return null;
|
|
11
|
-
return matches[0].record;
|
|
12
|
-
};
|