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
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { getMatchesByQueryString } from './query-string-matcher';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* TODO refaktor jako v getu do MatchItem
|
|
5
|
-
*/
|
|
6
|
-
export const matchRecordPost = (req, bundle) => {
|
|
7
|
-
let matches = getMatchesByQueryString(req, bundle);
|
|
8
|
-
matches.sort((a, b) => {
|
|
9
|
-
return b.result.compare(a.result);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
let requestBody = req.body;
|
|
13
|
-
|
|
14
|
-
// console.log('***********');
|
|
15
|
-
// console.log(requestBody);
|
|
16
|
-
// console.log('=========== matches count: '+matches.length);
|
|
17
|
-
// myLog(matches);
|
|
18
|
-
// console.log('..................');
|
|
19
|
-
|
|
20
|
-
let searchedRecord = null;
|
|
21
|
-
let searchedRecordDefault = null;
|
|
22
|
-
let searchedRecordRegex = null;
|
|
23
|
-
matches.forEach(({ record }) => {
|
|
24
|
-
let { body } = record;
|
|
25
|
-
if (!!body) {
|
|
26
|
-
if (!!body.type && !!body.value) {
|
|
27
|
-
switch (body.type) {
|
|
28
|
-
case 'REGEX': {
|
|
29
|
-
let value = body.value;
|
|
30
|
-
|
|
31
|
-
var re = new RegExp(value, 'g');
|
|
32
|
-
var str = JSON.stringify(requestBody);
|
|
33
|
-
var result = str.match(re);
|
|
34
|
-
// console.log('+++++++++++++');
|
|
35
|
-
// console.log(str);
|
|
36
|
-
// console.log(value);
|
|
37
|
-
// console.log(result);
|
|
38
|
-
|
|
39
|
-
let isMatched = !!result;
|
|
40
|
-
if (isMatched && !searchedRecordRegex) {
|
|
41
|
-
searchedRecordRegex = record;
|
|
42
|
-
}
|
|
43
|
-
// console.log('REGEX '+!!result);
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
case 'JSON': {
|
|
47
|
-
// match only filled
|
|
48
|
-
if (body.matchType && body.matchType === 'ONLY_MATCHING_FIELDS') {
|
|
49
|
-
let recordBodyValue = JSON.parse(body.value);
|
|
50
|
-
|
|
51
|
-
// let requestBodyKeysLength = Object.keys(requestBody).length;
|
|
52
|
-
// let recordBodyKeysLength = Object.keys(recordBodyValue).length;
|
|
53
|
-
// if(requestBodyKeysLength === recordBodyKeysLength){
|
|
54
|
-
let isMatched = true;
|
|
55
|
-
Object.keys(recordBodyValue).forEach((key) => {
|
|
56
|
-
let value = recordBodyValue[key];
|
|
57
|
-
if (
|
|
58
|
-
JSON.stringify(value) !== JSON.stringify(requestBody[key])
|
|
59
|
-
) {
|
|
60
|
-
isMatched = false;
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
if (isMatched && !searchedRecord) {
|
|
64
|
-
searchedRecord = record;
|
|
65
|
-
}
|
|
66
|
-
// console.log('TEST FOR MATCH = '+isMatched);
|
|
67
|
-
// }else{
|
|
68
|
-
// console.log('NO MATCH');
|
|
69
|
-
// }
|
|
70
|
-
break; // break only if type is ONLY_MATCHING_FIELDS
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
case 'STRING':
|
|
74
|
-
default: {
|
|
75
|
-
// exact match
|
|
76
|
-
// console.log('******');
|
|
77
|
-
// console.log(body.value);
|
|
78
|
-
// console.log('------------');
|
|
79
|
-
// console.log(JSON.stringify(requestBody));
|
|
80
|
-
// console.log('=============');
|
|
81
|
-
// console.log(body.value === JSON.stringify(requestBody));
|
|
82
|
-
|
|
83
|
-
let isMatched = body.value === JSON.stringify(requestBody);
|
|
84
|
-
if (isMatched && !searchedRecord) {
|
|
85
|
-
searchedRecord = record;
|
|
86
|
-
}
|
|
87
|
-
// console.log('EXACT DEFAULT '+isMatched);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
let isMatched = body === JSON.stringify(requestBody);
|
|
92
|
-
if (isMatched && !searchedRecord) {
|
|
93
|
-
searchedRecord = record;
|
|
94
|
-
}
|
|
95
|
-
// console.log('EXACT');
|
|
96
|
-
// exact match
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
let isRequestBodyEmpty =
|
|
100
|
-
!requestBody || JSON.stringify(requestBody) === '{}';
|
|
101
|
-
// console.log('ELSE');
|
|
102
|
-
// console.log('isRequestBodyEmpty '+isRequestBodyEmpty);
|
|
103
|
-
// console.log(requestBody);
|
|
104
|
-
if (isRequestBodyEmpty && !searchedRecord) {
|
|
105
|
-
searchedRecord = record;
|
|
106
|
-
} else if (!searchedRecordDefault) {
|
|
107
|
-
searchedRecordDefault = record;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// myLog(record);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
if (!searchedRecord) {
|
|
115
|
-
searchedRecord = searchedRecordRegex;
|
|
116
|
-
}
|
|
117
|
-
if (!searchedRecord) {
|
|
118
|
-
searchedRecord = searchedRecordDefault;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// console.log('POST RESULT'.yellow);
|
|
122
|
-
// console.log(JSON.stringify(searchedRecord, null, " "));
|
|
123
|
-
return searchedRecord;
|
|
124
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import MatchItem from './MatchItem';
|
|
2
|
-
import { REG_ALL } from '../../constants';
|
|
3
|
-
import MatchingResult, {
|
|
4
|
-
MATCH_DEFAULT,
|
|
5
|
-
MATCH_NO,
|
|
6
|
-
MATCH_STRICT,
|
|
7
|
-
MATCH_TYPE,
|
|
8
|
-
} from './MatchingResult';
|
|
9
|
-
|
|
10
|
-
const TYPE_SPECIFIC_REGEX = 'SPECIFIC_REGEX';
|
|
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
|
-
|
|
24
|
-
/**
|
|
25
|
-
* exist dont exist dont
|
|
26
|
-
* req/master 0 1 0 1
|
|
27
|
-
* bundle/slave 0 0 1 1
|
|
28
|
-
* result = strict default no subset/no
|
|
29
|
-
*
|
|
30
|
-
* @param {Object} masterQS request
|
|
31
|
-
* @param {Array.<Object>} slaveQS bundle
|
|
32
|
-
*/
|
|
33
|
-
export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
|
|
34
|
-
let isMasterExist = !!masterQS && JSON.stringify(masterQS) !== '{}';
|
|
35
|
-
let isSlaveExist = !!slaveQS;
|
|
36
|
-
// console.log('+++++++++++'.green);
|
|
37
|
-
// console.log(masterQS);
|
|
38
|
-
// console.log(slaveQS);
|
|
39
|
-
// console.log('+++++++++++'.blue);
|
|
40
|
-
if (!isMasterExist && !isSlaveExist) return MATCH_STRICT;
|
|
41
|
-
if (!isMasterExist && isSlaveExist) return MATCH_NO;
|
|
42
|
-
if (isMasterExist && !isSlaveExist) return MATCH_DEFAULT;
|
|
43
|
-
let result = null;
|
|
44
|
-
let countOfMatches = 0;
|
|
45
|
-
let countOfMatchesUnspecific = 0;
|
|
46
|
-
slaveQS.forEach(({ name, values, type }) => {
|
|
47
|
-
if (!masterQS[name]) {
|
|
48
|
-
result = MATCH_NO;
|
|
49
|
-
} else {
|
|
50
|
-
let isContained = values.indexOf(masterQS[name]) !== -1;
|
|
51
|
-
|
|
52
|
-
let isSpecific = values.indexOf(REG_ALL) === -1;
|
|
53
|
-
if (type === TYPE_SPECIFIC_REGEX) {
|
|
54
|
-
isSpecific = true;
|
|
55
|
-
if (!isContained) {
|
|
56
|
-
values.forEach((value) => {
|
|
57
|
-
let regex = new RegExp(value);
|
|
58
|
-
|
|
59
|
-
// console.log(name);
|
|
60
|
-
// console.log(regex);
|
|
61
|
-
// console.log(masterQS[name]);
|
|
62
|
-
// console.log('regex.test(masterQS[name]) ' +regex.test(masterQS[name]));
|
|
63
|
-
if (regex.test(masterQS[name])) {
|
|
64
|
-
isSpecific = false;
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (isSpecific && !isContained) {
|
|
71
|
-
if (isEqualQueryParamValues(masterQS[name], values)) {
|
|
72
|
-
countOfMatches++;
|
|
73
|
-
} else {
|
|
74
|
-
result = MATCH_NO;
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
if (!isSpecific) countOfMatchesUnspecific++;
|
|
78
|
-
countOfMatches++;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (result !== null) return result;
|
|
84
|
-
|
|
85
|
-
let masterQSLength = Object.keys(masterQS).length;
|
|
86
|
-
let slaveQSLength = slaveQS.length;
|
|
87
|
-
|
|
88
|
-
if (slaveQSLength === countOfMatches && countOfMatches === masterQSLength)
|
|
89
|
-
return MATCH_STRICT;
|
|
90
|
-
return new MatchingResult(
|
|
91
|
-
MATCH_TYPE.SUBSET,
|
|
92
|
-
countOfMatches,
|
|
93
|
-
countOfMatchesUnspecific
|
|
94
|
-
);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export const getMatchesByQueryString = (req, definitions) => {
|
|
98
|
-
let matches = [];
|
|
99
|
-
definitions.forEach((record) => {
|
|
100
|
-
// console.log(req.query);
|
|
101
|
-
// console.log(record.queryStringParameters);
|
|
102
|
-
let result = getMatchingResultByQueryStrings(
|
|
103
|
-
req.query,
|
|
104
|
-
record.queryStringParameters
|
|
105
|
-
);
|
|
106
|
-
// console.log('result');
|
|
107
|
-
// console.log(result);
|
|
108
|
-
if (!result.isNo()) {
|
|
109
|
-
matches.push(new MatchItem(record, result));
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
return matches;
|
|
113
|
-
};
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { StatusCodes } from 'http-status-codes';
|
|
2
|
-
import { methods } from '../constants';
|
|
3
|
-
import { isFunction } from './utils';
|
|
4
|
-
import { matchRecordPost } from './matchers/http-post-matcher';
|
|
5
|
-
import { matchRecordGet } from './matchers/http-get-matcher';
|
|
6
|
-
|
|
7
|
-
const APPLICATION_JSON = 'application/json';
|
|
8
|
-
|
|
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) => {
|
|
53
|
-
let method = req.method;
|
|
54
|
-
let path = req.route.path;
|
|
55
|
-
let definitions = sourcesParser.getMap()[path][method];
|
|
56
|
-
|
|
57
|
-
// console.log(method);
|
|
58
|
-
// console.log(req.body);
|
|
59
|
-
|
|
60
|
-
let response = null;
|
|
61
|
-
let matchedRecord = null;
|
|
62
|
-
|
|
63
|
-
if (definitions.length === 1) {
|
|
64
|
-
matchedRecord = definitions[0];
|
|
65
|
-
response = matchedRecord.response;
|
|
66
|
-
} else {
|
|
67
|
-
switch (method) {
|
|
68
|
-
case methods.POST: {
|
|
69
|
-
matchedRecord = matchRecordPost(req, definitions);
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
case methods.GET:
|
|
74
|
-
case methods.PUT:
|
|
75
|
-
case methods.DELETE:
|
|
76
|
-
case methods.PATCH:
|
|
77
|
-
default: {
|
|
78
|
-
matchedRecord = matchRecordGet(req, definitions);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (matchedRecord !== null) {
|
|
83
|
-
response = matchedRecord.response;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (response === null) {
|
|
87
|
-
console.log('ERR no reaponse but why?'.bgRed.white);
|
|
88
|
-
console.log('I know this URL but no match for parameters.'.bgBlue.white);
|
|
89
|
-
console.log(`method ${method} path ${path}`);
|
|
90
|
-
console.log('QUERY'.yellow);
|
|
91
|
-
console.log(req.query);
|
|
92
|
-
console.log('BODY'.yellow);
|
|
93
|
-
console.log(req.body);
|
|
94
|
-
console.log('bundle'.yellow);
|
|
95
|
-
// myLog(bundle);
|
|
96
|
-
res
|
|
97
|
-
.status(StatusCodes.NOT_FOUND)
|
|
98
|
-
.send('NOT FOUND - no reaponse but why? Look to console.')
|
|
99
|
-
.end();
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (isFunction(response)) {
|
|
105
|
-
if (matchedRecord.useFullRequestInResponse) {
|
|
106
|
-
response = response(req);
|
|
107
|
-
} else {
|
|
108
|
-
response = response(req.params, req.query, req.body, req.headers);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!!response.headers) {
|
|
113
|
-
response.headers.forEach((headerItem) => {
|
|
114
|
-
res.setHeader(headerItem['name'], headerItem['values'][0]);
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
res
|
|
119
|
-
.status(response.statusCode)
|
|
120
|
-
.type(response.contentType || APPLICATION_JSON);
|
|
121
|
-
|
|
122
|
-
if (response.delay) {
|
|
123
|
-
setTimeout(() => {
|
|
124
|
-
sendResponse(response, res, next);
|
|
125
|
-
}, response.delay);
|
|
126
|
-
} else {
|
|
127
|
-
sendResponse(response, res, next);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { methods, REG_ALL } from '../constants';
|
|
2
|
-
import config from './../config';
|
|
3
|
-
|
|
4
|
-
const { isLogEnabled } = config;
|
|
5
|
-
|
|
6
|
-
class Record {
|
|
7
|
-
constructor(record) {
|
|
8
|
-
this.__record = record;
|
|
9
|
-
this.useFullRequestInResponse = record.request.useFullRequestInResponse;
|
|
10
|
-
this.queryStringParameters = record.request.queryStringParameters;
|
|
11
|
-
this.body = record.request.body;
|
|
12
|
-
this.response = record.response;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Class for handling sources
|
|
18
|
-
*/
|
|
19
|
-
export class SourcesParser {
|
|
20
|
-
constructor(sources) {
|
|
21
|
-
this.sources = sources;
|
|
22
|
-
this.generateMapFromSources(sources);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @public
|
|
27
|
-
* @return {!Object}
|
|
28
|
-
*/
|
|
29
|
-
getMap() {
|
|
30
|
-
return this.map;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @public
|
|
35
|
-
*/
|
|
36
|
-
resetMap() {
|
|
37
|
-
this.generateMapFromSources(this.sources);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Method to initialize sources to map
|
|
42
|
-
* @param {Object} sources
|
|
43
|
-
*/
|
|
44
|
-
generateMapFromSources(sources) {
|
|
45
|
-
const GENERATE_TITLE = 'Generate original map from sources';
|
|
46
|
-
if (isLogEnabled) {
|
|
47
|
-
console.time(GENERATE_TITLE);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this.map = {};
|
|
51
|
-
sources.forEach((source) => {
|
|
52
|
-
this.setSource(this.map, source);
|
|
53
|
-
});
|
|
54
|
-
this.sortMap();
|
|
55
|
-
|
|
56
|
-
if (isLogEnabled) {
|
|
57
|
-
console.timeEnd(GENERATE_TITLE);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
*/
|
|
64
|
-
setSource(map, source) {
|
|
65
|
-
source.forEach((record) => {
|
|
66
|
-
this.setRecord(map, record);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
*
|
|
72
|
-
*/
|
|
73
|
-
switchRecord(record) {
|
|
74
|
-
const SET_NEW_REC = 'Set new record';
|
|
75
|
-
if (isLogEnabled) console.time(SET_NEW_REC);
|
|
76
|
-
let { method, path, queryStringParameters, body } = record.request;
|
|
77
|
-
let isChanged = false;
|
|
78
|
-
this.map[path][method].forEach((changedRecord, index) => {
|
|
79
|
-
// console.log('**************'.blue);
|
|
80
|
-
// console.log(JSON.stringify(body));
|
|
81
|
-
// console.log(JSON.stringify(changedRecord.body));
|
|
82
|
-
// console.log(JSON.stringify(body) == JSON.stringify(changedRecord.body));
|
|
83
|
-
if (
|
|
84
|
-
isQueryStringEqual(
|
|
85
|
-
queryStringParameters,
|
|
86
|
-
changedRecord.queryStringParameters
|
|
87
|
-
) &&
|
|
88
|
-
JSON.stringify(body) === JSON.stringify(changedRecord.body)
|
|
89
|
-
) {
|
|
90
|
-
// console.log('CHANGE FROM '.green);
|
|
91
|
-
// console.log(JSON.stringify(this.map[path][method][index], null, " "));
|
|
92
|
-
this.map[path][method][index] = new Record(record);
|
|
93
|
-
// console.log('CHANGE TO '.blue);
|
|
94
|
-
// console.log(JSON.stringify(this.map[path][method][index], null, " "));
|
|
95
|
-
isChanged = true;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
if (!isChanged) {
|
|
99
|
-
this.setRecord(this.map, record);
|
|
100
|
-
}
|
|
101
|
-
this.sortMap();
|
|
102
|
-
if (isLogEnabled) console.timeEnd(SET_NEW_REC);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
*
|
|
107
|
-
*/
|
|
108
|
-
setRecord(map, record) {
|
|
109
|
-
let { method, path } = record.request;
|
|
110
|
-
if (!map[path]) map[path] = {};
|
|
111
|
-
if (!map[path][method]) map[path][method] = [];
|
|
112
|
-
map[path][method].push(new Record(record));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Method to sort map
|
|
117
|
-
*/
|
|
118
|
-
sortMap() {
|
|
119
|
-
Object.keys(this.map).forEach((path) => {
|
|
120
|
-
let pathItem = this.map[path];
|
|
121
|
-
let gets = pathItem[methods.GET];
|
|
122
|
-
if (gets) {
|
|
123
|
-
gets.sort(compare);
|
|
124
|
-
}
|
|
125
|
-
let posts = pathItem[methods.POST];
|
|
126
|
-
if (posts) {
|
|
127
|
-
posts.sort(compare);
|
|
128
|
-
}
|
|
129
|
-
let puts = pathItem[methods.PUT];
|
|
130
|
-
if (puts) {
|
|
131
|
-
puts.sort(compare);
|
|
132
|
-
}
|
|
133
|
-
let deletes = pathItem[methods.DELETE];
|
|
134
|
-
if (deletes) {
|
|
135
|
-
deletes.sort(compare);
|
|
136
|
-
}
|
|
137
|
-
let patches = pathItem[methods.PATCH];
|
|
138
|
-
if (patches) {
|
|
139
|
-
patches.sort(compare);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* TODO: refactoring
|
|
147
|
-
*/
|
|
148
|
-
function isQueryStringEqual(qs1, qs2) {
|
|
149
|
-
if ((qs1 && !qs2) || (!qs1 && qs2)) return false;
|
|
150
|
-
if (!qs1 && !qs2) return true;
|
|
151
|
-
if (qs1.length !== qs2.length) return false;
|
|
152
|
-
let isEqual = true;
|
|
153
|
-
let qs1ref = {};
|
|
154
|
-
let qs2ref = {};
|
|
155
|
-
|
|
156
|
-
qs1.forEach((item) => {
|
|
157
|
-
qs1ref[item['name']] = item['values'];
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
qs2.forEach((item) => {
|
|
161
|
-
qs2ref[item['name']] = item['values'];
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
Object.keys(qs1ref).forEach((name) => {
|
|
165
|
-
let values = qs1ref[name];
|
|
166
|
-
let qs2Values = qs2ref[name];
|
|
167
|
-
if (qs2Values.length != values.length) {
|
|
168
|
-
isEqual = false;
|
|
169
|
-
} else {
|
|
170
|
-
values.forEach((value) => {
|
|
171
|
-
if (qs2Values.indexOf(value) === -1) {
|
|
172
|
-
isEqual = false;
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return isEqual;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Method to compare two requests for sorting by params count
|
|
182
|
-
*/
|
|
183
|
-
function compare(a, b) {
|
|
184
|
-
if (!a.queryStringParameters && !b.queryStringParameters) return 0;
|
|
185
|
-
if (!a.queryStringParameters) return 1;
|
|
186
|
-
if (!b.queryStringParameters) return -1;
|
|
187
|
-
if (b.queryStringParameters.length != a.queryStringParameters.length)
|
|
188
|
-
return b.queryStringParameters.length - a.queryStringParameters.length;
|
|
189
|
-
|
|
190
|
-
// toto posledni pravidlo je kvuli obecnemu parametru, aby prevazili konkretni nad obecnym. 2 > .*
|
|
191
|
-
let aSpecificCount = 0;
|
|
192
|
-
let bSpecificCount = 0;
|
|
193
|
-
a.queryStringParameters.forEach((qs, index) => {
|
|
194
|
-
if (qs['values'].indexOf(REG_ALL) !== -1) aSpecificCount++;
|
|
195
|
-
});
|
|
196
|
-
b.queryStringParameters.forEach((qs) => {
|
|
197
|
-
if (qs['values'].indexOf(REG_ALL) !== -1) bSpecificCount++;
|
|
198
|
-
});
|
|
199
|
-
return aSpecificCount - bSpecificCount;
|
|
200
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { SourcesParser } from './sources-parser';
|
|
2
|
-
import assert from 'assert';
|
|
3
|
-
|
|
4
|
-
describe('source-parser', () => {
|
|
5
|
-
it('should parse source', () => {
|
|
6
|
-
// given
|
|
7
|
-
const sources = [
|
|
8
|
-
[
|
|
9
|
-
{
|
|
10
|
-
request: {
|
|
11
|
-
method: 'GET',
|
|
12
|
-
path: '/api/test',
|
|
13
|
-
},
|
|
14
|
-
response: {
|
|
15
|
-
statusCode: 200,
|
|
16
|
-
body: JSON.stringify({
|
|
17
|
-
key: 'value',
|
|
18
|
-
}),
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
],
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
// when
|
|
25
|
-
const parser = new SourcesParser(sources);
|
|
26
|
-
|
|
27
|
-
// then
|
|
28
|
-
assert.equal(Object.keys(parser.map).length, 1);
|
|
29
|
-
assert.equal(Object.keys(parser.map)[0], '/api/test');
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { methods } from '../constants';
|
|
2
|
-
import { requestHandler } from './request-handler';
|
|
3
|
-
|
|
4
|
-
export class SourcesRouter {
|
|
5
|
-
/**
|
|
6
|
-
* @param {SourcesParser} parser
|
|
7
|
-
*/
|
|
8
|
-
constructor(parser) {
|
|
9
|
-
this.parser = parser;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
registerSources(app, isLogEnabled) {
|
|
13
|
-
const map = this.parser.getMap();
|
|
14
|
-
Object.keys(map).forEach((path) => {
|
|
15
|
-
this.registerGET(app, path, isLogEnabled);
|
|
16
|
-
this.registerPOST(app, path, isLogEnabled);
|
|
17
|
-
this.registerPUT(app, path, isLogEnabled);
|
|
18
|
-
this.registerDELETE(app, path, isLogEnabled);
|
|
19
|
-
this.registerPATCH(app, path, isLogEnabled);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
registerGET(app, path, map, isLogEnabled) {
|
|
24
|
-
const pathItem = this.parser.getMap()[path];
|
|
25
|
-
const gets = pathItem[methods.GET];
|
|
26
|
-
|
|
27
|
-
if (gets) {
|
|
28
|
-
gets.forEach(() => {
|
|
29
|
-
app.get(path, (req, res, next) => {
|
|
30
|
-
requestHandler(this.parser, req, res, next);
|
|
31
|
-
});
|
|
32
|
-
this.logRegisteredPath(isLogEnabled, methods.GET, path);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
registerPOST(app, path, map, isLogEnabled) {
|
|
38
|
-
const pathItem = this.parser.getMap()[path];
|
|
39
|
-
const posts = pathItem[methods.POST];
|
|
40
|
-
if (posts) {
|
|
41
|
-
posts.forEach(() => {
|
|
42
|
-
app.post(path, (req, res, next) => {
|
|
43
|
-
requestHandler(this.parser, req, res, next);
|
|
44
|
-
});
|
|
45
|
-
this.logRegisteredPath(isLogEnabled, methods.POST, path);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
registerPUT(app, path, map, isLogEnabled) {
|
|
51
|
-
const pathItem = this.parser.getMap()[path];
|
|
52
|
-
const puts = pathItem[methods.PUT];
|
|
53
|
-
if (puts) {
|
|
54
|
-
puts.forEach(() => {
|
|
55
|
-
app.put(path, (req, res, next) => {
|
|
56
|
-
requestHandler(this.parser, req, res, next);
|
|
57
|
-
});
|
|
58
|
-
this.logRegisteredPath(isLogEnabled, methods.PUT, path);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
registerDELETE(app, path, map, isLogEnabled) {
|
|
64
|
-
const pathItem = this.parser.getMap()[path];
|
|
65
|
-
const deletes = pathItem[methods.DELETE];
|
|
66
|
-
if (deletes) {
|
|
67
|
-
deletes.forEach(() => {
|
|
68
|
-
app.delete(path, (req, res, next) => {
|
|
69
|
-
requestHandler(this.parser, req, res, next);
|
|
70
|
-
});
|
|
71
|
-
this.logRegisteredPath(isLogEnabled, methods.DELETE, path);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
registerPATCH(app, path, map, isLogEnabled) {
|
|
77
|
-
const pathItem = this.parser.getMap()[path];
|
|
78
|
-
const patches = pathItem[methods.PATCH];
|
|
79
|
-
if (patches) {
|
|
80
|
-
patches.forEach(() => {
|
|
81
|
-
app.patch(path, (req, res, next) => {
|
|
82
|
-
requestHandler(this.parser, req, res, next);
|
|
83
|
-
});
|
|
84
|
-
this.logRegisteredPath(isLogEnabled, methods.PATCH, path);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Method to log registered path
|
|
91
|
-
*/
|
|
92
|
-
logRegisteredPath(isLogEnabled, method, path) {
|
|
93
|
-
if (isLogEnabled) {
|
|
94
|
-
console.log(`REG ${method} ${path}`.magenta);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|