express-mock-server 3.3.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.map +1 -1
- 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 +3 -3
- 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.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 +5 -5
- 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 +4 -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 +5 -4
- package/package.json +15 -15
- package/test/image.jpg +0 -0
- package/test/mock-api.spec.js +14 -0
- package/test/sources.js +37 -21
- package/test/urls.js +1 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,CAACf,MAAD,CAAzB;AACAK,IAAAA,GAAG,CAACW,gBAAJ,CAAqBf,QAArB;AACD;;AAED,SAAOD,MAAP;AACD;AAED;AACA;AACA;;;AACO,SAASiB,UAAT,GAAsB;AAC3B,MAAIjB,MAAJ,EAAY;AACVA,IAAAA,MAAM,CAACkB,KAAP;AACD;;AACD,MAAIjB,QAAJ,EAAc;AACZA,IAAAA,QAAQ,CAACiB,KAAT;AACD;AACF;;AAED,SAASH,cAAT,CAAwBf,MAAxB,EAAgC;AAC9B,QAAMC,QAAQ,GAAG,IAAIkB,YAAGC,MAAP,CAAc;AAAEC,IAAAA,QAAQ,EAAE;AAAZ,GAAd,CAAjB;AACApB,EAAAA,QAAQ,CAACqB,EAAT,CAAY,YAAZ,EAA2BC,MAAD,IAAY;AACpCd,IAAAA,OAAO,CAACC,GAAR,CAAY,4BAAZ;AACAa,IAAAA,MAAM,CAACD,EAAP,CAAU,SAAV,EAAsBE,OAAD,IAAa;AAChCf,MAAAA,OAAO,CAACC,GAAR,CAAY,mCAAZ;AACAD,MAAAA,OAAO,CAACC,GAAR,CAAYc,OAAZ;AACD,KAHD;AAIAD,IAAAA,MAAM,CAACD,EAAP,CAAU,OAAV,EAAmB,MAAMb,OAAO,CAACC,GAAR,CAAY,+BAAZ,CAAzB;AACD,GAPD;AASAV,EAAAA,MAAM,CAACsB,EAAP,CAAU,SAAV,EAAqB,CAACG,OAAD,EAAUF,MAAV,EAAkBG,IAAlB,KAA2B;AAC9CzB,IAAAA,QAAQ,CAAC0B,aAAT,CAAuBF,OAAvB,EAAgCF,MAAhC,EAAwCG,IAAxC,EAA+CH,MAAD,IAAY;AACxDtB,MAAAA,QAAQ,CAAC2B,IAAT,CAAc,YAAd,EAA4BL,MAA5B,EAAoCE,OAApC;AACD,KAFD;AAGD,GAJD;AAMA,SAAOxB,QAAP;AACD;;AAED,SAASM,aAAT,CAAuBH,YAAvB,EAAqC;AACnC,SAAOA,YAAY,CAACN,IAAb,GAAoBM,YAAY,CAACN,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
|
};
|
|
@@ -2,24 +2,24 @@ import express from 'express';
|
|
|
2
2
|
|
|
3
3
|
const router = express.Router({});
|
|
4
4
|
|
|
5
|
-
export const websocketApi = wsServer => {
|
|
5
|
+
export const websocketApi = (wsServer) => {
|
|
6
6
|
router.post('/websocket/clients/message', (request, response) => {
|
|
7
|
-
wsServer.clients.forEach(ws => {
|
|
7
|
+
wsServer.clients.forEach((ws) => {
|
|
8
8
|
ws.send(JSON.stringify(request.body));
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
response.send({
|
|
12
|
-
status: true
|
|
12
|
+
status: true,
|
|
13
13
|
});
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
router.post('/websocket/clients/disconnect', (request, response) => {
|
|
17
|
-
wsServer.clients.forEach(ws => {
|
|
17
|
+
wsServer.clients.forEach((ws) => {
|
|
18
18
|
ws.close();
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
response.send({
|
|
22
|
-
status: true
|
|
22
|
+
status: true,
|
|
23
23
|
});
|
|
24
24
|
});
|
|
25
25
|
|
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,7 +4,7 @@ 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';
|
|
@@ -19,7 +19,7 @@ const isEqualQueryParamValues = (masterValues, slaveValues) => {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
return [...masterValues].sort().join('') === [...slaveValues].sort().join('');
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* exist dont exist dont
|
|
@@ -53,7 +53,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
|
53
53
|
if (type === TYPE_SPECIFIC_REGEX) {
|
|
54
54
|
isSpecific = true;
|
|
55
55
|
if (!isContained) {
|
|
56
|
-
values.forEach(value => {
|
|
56
|
+
values.forEach((value) => {
|
|
57
57
|
let regex = new RegExp(value);
|
|
58
58
|
|
|
59
59
|
// console.log(name);
|
|
@@ -96,7 +96,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
|
96
96
|
|
|
97
97
|
export const getMatchesByQueryString = (req, definitions) => {
|
|
98
98
|
let matches = [];
|
|
99
|
-
definitions.forEach(record => {
|
|
99
|
+
definitions.forEach((record) => {
|
|
100
100
|
// console.log(req.query);
|
|
101
101
|
// console.log(record.queryStringParameters);
|
|
102
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
|
|
@@ -11,7 +11,7 @@ export class SourcesRouter {
|
|
|
11
11
|
|
|
12
12
|
registerSources(app, isLogEnabled) {
|
|
13
13
|
const map = this.parser.getMap();
|
|
14
|
-
Object.keys(map).forEach(path => {
|
|
14
|
+
Object.keys(map).forEach((path) => {
|
|
15
15
|
this.registerGET(app, path, isLogEnabled);
|
|
16
16
|
this.registerPOST(app, path, isLogEnabled);
|
|
17
17
|
this.registerPUT(app, path, isLogEnabled);
|
|
@@ -26,8 +26,8 @@ export class SourcesRouter {
|
|
|
26
26
|
|
|
27
27
|
if (gets) {
|
|
28
28
|
gets.forEach(() => {
|
|
29
|
-
app.get(path, (req, res) => {
|
|
30
|
-
requestHandler(this.parser, req, res);
|
|
29
|
+
app.get(path, (req, res, next) => {
|
|
30
|
+
requestHandler(this.parser, req, res, next);
|
|
31
31
|
});
|
|
32
32
|
this.logRegisteredPath(isLogEnabled, methods.GET, path);
|
|
33
33
|
});
|
|
@@ -39,8 +39,8 @@ export class SourcesRouter {
|
|
|
39
39
|
const posts = pathItem[methods.POST];
|
|
40
40
|
if (posts) {
|
|
41
41
|
posts.forEach(() => {
|
|
42
|
-
app.post(path, (req, res) => {
|
|
43
|
-
requestHandler(this.parser, req, res);
|
|
42
|
+
app.post(path, (req, res, next) => {
|
|
43
|
+
requestHandler(this.parser, req, res, next);
|
|
44
44
|
});
|
|
45
45
|
this.logRegisteredPath(isLogEnabled, methods.POST, path);
|
|
46
46
|
});
|
|
@@ -52,8 +52,8 @@ export class SourcesRouter {
|
|
|
52
52
|
const puts = pathItem[methods.PUT];
|
|
53
53
|
if (puts) {
|
|
54
54
|
puts.forEach(() => {
|
|
55
|
-
app.put(path, (req, res) => {
|
|
56
|
-
requestHandler(this.parser, req, res);
|
|
55
|
+
app.put(path, (req, res, next) => {
|
|
56
|
+
requestHandler(this.parser, req, res, next);
|
|
57
57
|
});
|
|
58
58
|
this.logRegisteredPath(isLogEnabled, methods.PUT, path);
|
|
59
59
|
});
|
|
@@ -65,8 +65,8 @@ export class SourcesRouter {
|
|
|
65
65
|
const deletes = pathItem[methods.DELETE];
|
|
66
66
|
if (deletes) {
|
|
67
67
|
deletes.forEach(() => {
|
|
68
|
-
app.delete(path, (req, res) => {
|
|
69
|
-
requestHandler(this.parser, req, res);
|
|
68
|
+
app.delete(path, (req, res, next) => {
|
|
69
|
+
requestHandler(this.parser, req, res, next);
|
|
70
70
|
});
|
|
71
71
|
this.logRegisteredPath(isLogEnabled, methods.DELETE, path);
|
|
72
72
|
});
|
|
@@ -78,8 +78,8 @@ export class SourcesRouter {
|
|
|
78
78
|
const patches = pathItem[methods.PATCH];
|
|
79
79
|
if (patches) {
|
|
80
80
|
patches.forEach(() => {
|
|
81
|
-
app.patch(path, (req, res) => {
|
|
82
|
-
requestHandler(this.parser, req, res);
|
|
81
|
+
app.patch(path, (req, res, next) => {
|
|
82
|
+
requestHandler(this.parser, req, res, next);
|
|
83
83
|
});
|
|
84
84
|
this.logRegisteredPath(isLogEnabled, methods.PATCH, path);
|
|
85
85
|
});
|
|
@@ -87,7 +87,7 @@ export class SourcesRouter {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* Method to log
|
|
90
|
+
* Method to log registered path
|
|
91
91
|
*/
|
|
92
92
|
logRegisteredPath(isLogEnabled, method, path) {
|
|
93
93
|
if (isLogEnabled) {
|
package/lib/core/utils.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -4,7 +4,8 @@ import { App } from './app';
|
|
|
4
4
|
import ws from 'ws';
|
|
5
5
|
|
|
6
6
|
const { port } = config;
|
|
7
|
-
let server,
|
|
7
|
+
let server,
|
|
8
|
+
wsServer = null;
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Method to start server.
|
|
@@ -46,9 +47,9 @@ export function serverStop() {
|
|
|
46
47
|
|
|
47
48
|
function startWebSocket(server) {
|
|
48
49
|
const wsServer = new ws.Server({ noServer: true });
|
|
49
|
-
wsServer.on('connection', socket => {
|
|
50
|
+
wsServer.on('connection', (socket) => {
|
|
50
51
|
console.log('WebSocket client connected');
|
|
51
|
-
socket.on('message', message => {
|
|
52
|
+
socket.on('message', (message) => {
|
|
52
53
|
console.log('WebSocket client message received');
|
|
53
54
|
console.log(message);
|
|
54
55
|
});
|
|
@@ -56,7 +57,7 @@ function startWebSocket(server) {
|
|
|
56
57
|
});
|
|
57
58
|
|
|
58
59
|
server.on('upgrade', (request, socket, head) => {
|
|
59
|
-
wsServer.handleUpgrade(request, socket, head, socket => {
|
|
60
|
+
wsServer.handleUpgrade(request, socket, head, (socket) => {
|
|
60
61
|
wsServer.emit('connection', socket, request);
|
|
61
62
|
});
|
|
62
63
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-mock-server",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Mock server powered by Express.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,24 +31,24 @@
|
|
|
31
31
|
"body-parser": "1.19.0",
|
|
32
32
|
"colors": "1.4.0",
|
|
33
33
|
"express": "4.17.1",
|
|
34
|
-
"http-status-codes": "1.4
|
|
35
|
-
"morgan": "1.
|
|
36
|
-
"nconf": "0.
|
|
37
|
-
"rimraf": "3.0.
|
|
38
|
-
"uuid": "
|
|
39
|
-
"ws": "7.4.
|
|
34
|
+
"http-status-codes": "2.1.4",
|
|
35
|
+
"morgan": "1.10.0",
|
|
36
|
+
"nconf": "0.11.3",
|
|
37
|
+
"rimraf": "3.0.2",
|
|
38
|
+
"uuid": "8.3.2",
|
|
39
|
+
"ws": "7.4.6"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@babel/cli": "7.
|
|
43
|
-
"@babel/core": "7.
|
|
44
|
-
"@babel/preset-env": "7.
|
|
45
|
-
"@babel/register": "7.
|
|
46
|
-
"chai": "4.
|
|
42
|
+
"@babel/cli": "7.16.0",
|
|
43
|
+
"@babel/core": "7.16.0",
|
|
44
|
+
"@babel/preset-env": "7.16.4",
|
|
45
|
+
"@babel/register": "7.16.0",
|
|
46
|
+
"chai": "4.3.4",
|
|
47
47
|
"chai-http": "4.3.0",
|
|
48
48
|
"mocha": "6.2.2",
|
|
49
|
-
"nodemon": "2.0.
|
|
50
|
-
"prettier": "
|
|
51
|
-
"request": "2.88.
|
|
49
|
+
"nodemon": "2.0.15",
|
|
50
|
+
"prettier": "2.5.1",
|
|
51
|
+
"request": "2.88.2",
|
|
52
52
|
"run-sequence": "2.2.1",
|
|
53
53
|
"websocket": "^1.0.34"
|
|
54
54
|
},
|
package/test/image.jpg
ADDED
|
Binary file
|
package/test/mock-api.spec.js
CHANGED
|
@@ -29,4 +29,18 @@ describe('API mocks', () => {
|
|
|
29
29
|
done();
|
|
30
30
|
});
|
|
31
31
|
});
|
|
32
|
+
|
|
33
|
+
it('should call mocked API with file response', done => {
|
|
34
|
+
chai
|
|
35
|
+
.request(server)
|
|
36
|
+
.get('/items/logo')
|
|
37
|
+
.end((err, res) => {
|
|
38
|
+
console.log(res.body);
|
|
39
|
+
should.not.exist(err);
|
|
40
|
+
res.status.should.equal(200);
|
|
41
|
+
res.type.should.equal('image/jpg');
|
|
42
|
+
chai.expect(res.body).to.have.lengthOf(2317786);
|
|
43
|
+
done();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
32
46
|
});
|