express-mock-server 3.4.2 → 3.5.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/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/app.js +7 -3
- package/dist/app.js.map +1 -1
- package/dist/config.js +24 -22
- package/dist/config.js.map +1 -1
- package/dist/core/matchers/query-string-matcher.js +1 -1
- package/dist/core/matchers/query-string-matcher.js.map +1 -1
- package/dist/core/request-handler.js +5 -1
- package/dist/core/request-handler.js.map +1 -1
- package/dist/core/sources-parser.js +2 -1
- package/dist/core/sources-parser.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.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 -72
- 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 -123
- package/lib/core/sources-parser.js +0 -199
- 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 -159
- package/test/urls.js +0 -14
- package/test/websocket-api.spec.js +0 -47
|
@@ -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,72 +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) {
|
|
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();
|
|
23
|
-
this.initControlApi();
|
|
24
|
-
this.initRecordingApi();
|
|
25
|
-
this.initMocks();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
initMiddleware() {
|
|
29
|
-
this.app.use(bodyParser.text());
|
|
30
|
-
this.app.use(bodyParser.json());
|
|
31
|
-
this.app.use(apiRecorder(this.apiUrl));
|
|
32
|
-
this.initLogger();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
initMocks() {
|
|
36
|
-
const router = new SourcesRouter(this.parser);
|
|
37
|
-
router.registerSources(this.app, isLogEnabled);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Method to start logger of requests
|
|
42
|
-
* Actual format
|
|
43
|
-
* 0.230 ms GET 200 /some/url/
|
|
44
|
-
* More option
|
|
45
|
-
* https://github.com/expressjs/morgan
|
|
46
|
-
*/
|
|
47
|
-
initLogger() {
|
|
48
|
-
if (isLogEnabled) {
|
|
49
|
-
this.app.use(morgan(':response-time ms :method :status :url'));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
initControlApi() {
|
|
54
|
-
this.app.use(this.apiUrl, controlApi(this.parser));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
initRecordingApi() {
|
|
58
|
-
this.app.use(this.apiUrl, recordingApi());
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
initWebSocketApi(wsServer) {
|
|
62
|
-
this.app.use(this.apiUrl, websocketApi(wsServer));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getApiUrl({ controlApiUrl } = {}) {
|
|
66
|
-
return controlApiUrl ? controlApiUrl : '/api/v1';
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
start(port, callback) {
|
|
70
|
-
return this.app.listen(port, callback);
|
|
71
|
-
}
|
|
72
|
-
}
|
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
|
-
};
|
|
@@ -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,123 +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
|
-
response = response(req.params, req.query, req.body, req.headers);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!!response.headers) {
|
|
109
|
-
response.headers.forEach((headerItem) => {
|
|
110
|
-
res.setHeader(headerItem['name'], headerItem['values'][0]);
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
res.status(response.statusCode).type(response.contentType || APPLICATION_JSON);
|
|
115
|
-
|
|
116
|
-
if (response.delay) {
|
|
117
|
-
setTimeout(() => {
|
|
118
|
-
sendResponse(response, res, next);
|
|
119
|
-
}, response.delay);
|
|
120
|
-
} else {
|
|
121
|
-
sendResponse(response, res, next);
|
|
122
|
-
}
|
|
123
|
-
};
|