express-mock-server 3.2.0 → 3.4.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/dist/api/control.js +2 -2
- package/dist/api/control.js.map +1 -1
- package/dist/api/recording/api-recorder.js +3 -5
- package/dist/api/recording/api-recorder.js.map +1 -1
- package/dist/api/recording/index.js.map +1 -1
- package/dist/api/websocket/index.js +35 -0
- package/dist/api/websocket/index.js.map +1 -0
- package/dist/app.js +10 -0
- package/dist/app.js.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/constants.js +1 -1
- package/dist/constants.js.map +1 -1
- package/dist/core/matchers/MatchItem.js.map +1 -1
- package/dist/core/matchers/MatchingResult.js +1 -1
- package/dist/core/matchers/MatchingResult.js.map +1 -1
- package/dist/core/matchers/http-post-matcher.js.map +1 -1
- package/dist/core/matchers/query-string-matcher.js +3 -3
- package/dist/core/matchers/query-string-matcher.js.map +1 -1
- package/dist/core/request-handler.js +42 -6
- package/dist/core/request-handler.js.map +1 -1
- package/dist/core/sources-parser.js.map +1 -1
- package/dist/core/sources-parser.spec.js.map +1 -1
- package/dist/core/sources-router.js +11 -11
- package/dist/core/sources-router.js.map +1 -1
- package/dist/core/utils.js.map +1 -1
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/lib/api/control.js +5 -5
- package/lib/api/recording/api-recorder.js +6 -6
- package/lib/api/recording/index.js +1 -1
- package/lib/api/websocket/index.js +27 -0
- package/lib/app.js +9 -0
- package/lib/config.js +1 -1
- package/lib/constants.js +1 -1
- package/lib/core/matchers/MatchingResult.js +1 -1
- package/lib/core/matchers/http-post-matcher.js +1 -1
- package/lib/core/matchers/query-string-matcher.js +4 -4
- package/lib/core/request-handler.js +51 -8
- package/lib/core/sources-parser.js +8 -8
- package/lib/core/sources-parser.spec.js +6 -6
- package/lib/core/sources-router.js +12 -12
- package/lib/core/utils.js +1 -1
- package/lib/index.js +33 -1
- package/package.json +17 -15
- package/src/index.js +1 -1
- package/test/image.jpg +0 -0
- package/test/index.js +15 -3
- package/test/mock-api.spec.js +14 -0
- package/test/sources.js +37 -21
- package/test/urls.js +1 -0
- package/test/websocket-api.spec.js +47 -0
package/lib/core/utils.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import 'colors';
|
|
2
2
|
import config from './config';
|
|
3
3
|
import { App } from './app';
|
|
4
|
+
import ws from 'ws';
|
|
4
5
|
|
|
5
6
|
const { port } = config;
|
|
6
|
-
let server
|
|
7
|
+
let server,
|
|
8
|
+
wsServer = null;
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Method to start server.
|
|
@@ -12,6 +14,7 @@ let server = null;
|
|
|
12
14
|
* @param {Object} serverConfig
|
|
13
15
|
* @param serverConfig.controlApiUrl ['/api/v1']
|
|
14
16
|
* @param serverConfig.port [8080]
|
|
17
|
+
* @param serverConfig.webSocketEnabled false
|
|
15
18
|
*/
|
|
16
19
|
export function serverStart(sources, serverConfig = {}) {
|
|
17
20
|
const app = new App(sources, serverConfig);
|
|
@@ -21,6 +24,12 @@ export function serverStart(sources, serverConfig = {}) {
|
|
|
21
24
|
console.log(`*************************`.yellow);
|
|
22
25
|
console.log('Server START'.bgGreen.white);
|
|
23
26
|
});
|
|
27
|
+
|
|
28
|
+
if (serverConfig.webSocketEnabled) {
|
|
29
|
+
wsServer = startWebSocket(server);
|
|
30
|
+
app.initWebSocketApi(wsServer);
|
|
31
|
+
}
|
|
32
|
+
|
|
24
33
|
return server;
|
|
25
34
|
}
|
|
26
35
|
|
|
@@ -31,6 +40,29 @@ export function serverStop() {
|
|
|
31
40
|
if (server) {
|
|
32
41
|
server.close();
|
|
33
42
|
}
|
|
43
|
+
if (wsServer) {
|
|
44
|
+
wsServer.close();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function startWebSocket(server) {
|
|
49
|
+
const wsServer = new ws.Server({ noServer: true });
|
|
50
|
+
wsServer.on('connection', (socket) => {
|
|
51
|
+
console.log('WebSocket client connected');
|
|
52
|
+
socket.on('message', (message) => {
|
|
53
|
+
console.log('WebSocket client message received');
|
|
54
|
+
console.log(message);
|
|
55
|
+
});
|
|
56
|
+
socket.on('close', () => console.log('WebSocket client disconnected'));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
server.on('upgrade', (request, socket, head) => {
|
|
60
|
+
wsServer.handleUpgrade(request, socket, head, (socket) => {
|
|
61
|
+
wsServer.emit('connection', socket, request);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return wsServer;
|
|
34
66
|
}
|
|
35
67
|
|
|
36
68
|
function getServerPort(serverConfig) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-mock-server",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.1",
|
|
4
4
|
"description": "Mock server powered by Express.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,24 +31,26 @@
|
|
|
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": "
|
|
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"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@babel/cli": "7.
|
|
42
|
-
"@babel/core": "7.
|
|
43
|
-
"@babel/preset-env": "7.
|
|
44
|
-
"@babel/register": "7.
|
|
45
|
-
"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",
|
|
46
47
|
"chai-http": "4.3.0",
|
|
47
48
|
"mocha": "6.2.2",
|
|
48
|
-
"nodemon": "2.0.
|
|
49
|
-
"prettier": "
|
|
50
|
-
"request": "2.88.
|
|
51
|
-
"run-sequence": "2.2.1"
|
|
49
|
+
"nodemon": "2.0.15",
|
|
50
|
+
"prettier": "2.5.1",
|
|
51
|
+
"request": "2.88.2",
|
|
52
|
+
"run-sequence": "2.2.1",
|
|
53
|
+
"websocket": "^1.0.34"
|
|
52
54
|
},
|
|
53
55
|
"babel": {
|
|
54
56
|
"presets": [
|
package/src/index.js
CHANGED
package/test/image.jpg
ADDED
|
Binary file
|
package/test/index.js
CHANGED
|
@@ -6,12 +6,15 @@ import sources from './sources';
|
|
|
6
6
|
import urls from './urls';
|
|
7
7
|
import responseKey, { responseKeyParam } from './responsekey';
|
|
8
8
|
import Digger from './Digger';
|
|
9
|
+
import { client as WebSocketClient } from 'websocket';
|
|
9
10
|
|
|
10
11
|
const PORT = 1337;
|
|
11
12
|
const SERVER_NAME = '127.0.0.1';
|
|
12
13
|
const SERVER_URL_WITHOUT_PORT = 'http://' + SERVER_NAME;
|
|
13
14
|
const SERVER_URL = SERVER_URL_WITHOUT_PORT + ':' + PORT;
|
|
14
15
|
|
|
16
|
+
const WS_SERVER_URL = 'ws://' + SERVER_NAME + ':' + PORT;
|
|
17
|
+
|
|
15
18
|
function createItemsExpectation(qs, key, done) {
|
|
16
19
|
http.get(SERVER_URL + urls.items + qs, res => {
|
|
17
20
|
new Digger(res, data => {
|
|
@@ -38,10 +41,8 @@ function createCodeExpectation(url, code, done) {
|
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
describe('server', () => {
|
|
41
|
-
|
|
42
|
-
|
|
43
44
|
before(() => {
|
|
44
|
-
serverStart(sources, { port: PORT });
|
|
45
|
+
serverStart(sources, { port: PORT, webSocketEnabled: true });
|
|
45
46
|
});
|
|
46
47
|
|
|
47
48
|
after(() => {
|
|
@@ -138,6 +139,17 @@ describe('server', () => {
|
|
|
138
139
|
});
|
|
139
140
|
});
|
|
140
141
|
|
|
142
|
+
describe('websocket', () => {
|
|
143
|
+
it('try to connect', done => {
|
|
144
|
+
const client = new WebSocketClient();
|
|
145
|
+
client.on('connect', function(connection) {
|
|
146
|
+
assert.equal('open', connection.state);
|
|
147
|
+
done();
|
|
148
|
+
});
|
|
149
|
+
client.connect(WS_SERVER_URL, 'echo-protocol');
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
141
153
|
function createDynamicResponseExpectation(key, done) {
|
|
142
154
|
const urlParamValue = 'urlParamValue';
|
|
143
155
|
|
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
|
});
|
package/test/sources.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import urls from './urls';
|
|
2
2
|
import responseKey, { responseKeyParam } from './responsekey';
|
|
3
|
+
import path from 'path';
|
|
3
4
|
|
|
4
5
|
const q1q2q3FilledQs = [
|
|
5
6
|
{ name: 'q1', values: ['.*'] },
|
|
6
7
|
{ name: 'q2', values: ['.*'] },
|
|
7
|
-
{ name: 'q3', values: ['.*'] }
|
|
8
|
+
{ name: 'q3', values: ['.*'] },
|
|
8
9
|
];
|
|
9
10
|
|
|
10
11
|
const q1filledQs = [{ name: 'q1', values: ['1'] }];
|
|
@@ -23,41 +24,41 @@ const sources = [
|
|
|
23
24
|
method: 'GET',
|
|
24
25
|
path: urls.items,
|
|
25
26
|
statusCode: 200,
|
|
26
|
-
key: responseKey.qsOptionalEmpty
|
|
27
|
+
key: responseKey.qsOptionalEmpty,
|
|
27
28
|
}),
|
|
28
29
|
createSource({
|
|
29
30
|
method: 'GET',
|
|
30
31
|
path: urls.items,
|
|
31
32
|
statusCode: 200,
|
|
32
33
|
key: responseKey.qsOptionalQ1Q2Q3Filed,
|
|
33
|
-
queryStringParameters: q1q2q3FilledQs
|
|
34
|
+
queryStringParameters: q1q2q3FilledQs,
|
|
34
35
|
}),
|
|
35
36
|
createSource({
|
|
36
37
|
method: 'GET',
|
|
37
38
|
path: urls.items,
|
|
38
39
|
statusCode: 200,
|
|
39
40
|
key: responseKey.qsOptionalQ1filled,
|
|
40
|
-
queryStringParameters: q1filledQs
|
|
41
|
+
queryStringParameters: q1filledQs,
|
|
41
42
|
}),
|
|
42
43
|
// item
|
|
43
44
|
createSource({
|
|
44
45
|
method: 'GET',
|
|
45
46
|
path: urls.item,
|
|
46
47
|
statusCode: 200,
|
|
47
|
-
key: responseKey.urlParamBase
|
|
48
|
+
key: responseKey.urlParamBase,
|
|
48
49
|
}),
|
|
49
50
|
createSource({
|
|
50
51
|
method: 'GET',
|
|
51
52
|
path: urls.itemHardId,
|
|
52
53
|
statusCode: 200,
|
|
53
|
-
key: responseKey.urlParamHardId
|
|
54
|
+
key: responseKey.urlParamHardId,
|
|
54
55
|
}),
|
|
55
56
|
createSource({
|
|
56
57
|
method: 'GET',
|
|
57
58
|
path: urls.items,
|
|
58
59
|
statusCode: 200,
|
|
59
60
|
key: responseKey.qsMultiQ1,
|
|
60
|
-
queryStringParameters: [{ name: 'q1', values: ['1', '2', '3'] }]
|
|
61
|
+
queryStringParameters: [{ name: 'q1', values: ['1', '2', '3'] }],
|
|
61
62
|
}),
|
|
62
63
|
|
|
63
64
|
// dynamic response
|
|
@@ -67,9 +68,24 @@ const sources = [
|
|
|
67
68
|
path: urls.dynamicResponse,
|
|
68
69
|
statusCode: 200,
|
|
69
70
|
key: responseKey.dynamicResponse,
|
|
70
|
-
queryStringParameters: dynamicResponseQs
|
|
71
|
+
queryStringParameters: dynamicResponseQs,
|
|
71
72
|
}),
|
|
72
|
-
|
|
73
|
+
|
|
74
|
+
// response with file
|
|
75
|
+
{
|
|
76
|
+
request: {
|
|
77
|
+
method: 'GET',
|
|
78
|
+
path: urls.itemsLogo,
|
|
79
|
+
},
|
|
80
|
+
response: {
|
|
81
|
+
statusCode: 200,
|
|
82
|
+
file: {
|
|
83
|
+
type: 'image/jpg',
|
|
84
|
+
path: path.join(__dirname, 'image.jpg'),
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
],
|
|
73
89
|
];
|
|
74
90
|
|
|
75
91
|
export default sources;
|
|
@@ -82,7 +98,7 @@ function createDynamicSource({
|
|
|
82
98
|
path,
|
|
83
99
|
statusCode,
|
|
84
100
|
key,
|
|
85
|
-
queryStringParameters = null
|
|
101
|
+
queryStringParameters = null,
|
|
86
102
|
}) {
|
|
87
103
|
let body = {};
|
|
88
104
|
body[responseKeyParam] = key;
|
|
@@ -95,22 +111,22 @@ function createDynamicSource({
|
|
|
95
111
|
body: {
|
|
96
112
|
type: 'JSON',
|
|
97
113
|
value: JSON.stringify({
|
|
98
|
-
bodyParam1: 'bodyValue1'
|
|
114
|
+
bodyParam1: 'bodyValue1',
|
|
99
115
|
}),
|
|
100
|
-
matchType: 'ONLY_MATCHING_FIELDS'
|
|
101
|
-
}
|
|
116
|
+
matchType: 'ONLY_MATCHING_FIELDS',
|
|
117
|
+
},
|
|
102
118
|
},
|
|
103
|
-
response: function(urlParams, qsParams, bodyParams) {
|
|
119
|
+
response: function (urlParams, qsParams, bodyParams) {
|
|
104
120
|
body['requestParams'] = {
|
|
105
121
|
qsParams,
|
|
106
122
|
bodyParams,
|
|
107
|
-
urlParams
|
|
123
|
+
urlParams,
|
|
108
124
|
};
|
|
109
125
|
return {
|
|
110
126
|
statusCode,
|
|
111
|
-
body: JSON.stringify(body)
|
|
127
|
+
body: JSON.stringify(body),
|
|
112
128
|
};
|
|
113
|
-
}
|
|
129
|
+
},
|
|
114
130
|
};
|
|
115
131
|
}
|
|
116
132
|
|
|
@@ -123,7 +139,7 @@ export function createSource({
|
|
|
123
139
|
statusCode,
|
|
124
140
|
delay,
|
|
125
141
|
key,
|
|
126
|
-
queryStringParameters = null
|
|
142
|
+
queryStringParameters = null,
|
|
127
143
|
}) {
|
|
128
144
|
let body = {};
|
|
129
145
|
body[responseKeyParam] = key;
|
|
@@ -132,12 +148,12 @@ export function createSource({
|
|
|
132
148
|
request: {
|
|
133
149
|
method,
|
|
134
150
|
path,
|
|
135
|
-
queryStringParameters
|
|
151
|
+
queryStringParameters,
|
|
136
152
|
},
|
|
137
153
|
response: {
|
|
138
154
|
delay,
|
|
139
155
|
statusCode,
|
|
140
|
-
body: JSON.stringify(body)
|
|
141
|
-
}
|
|
156
|
+
body: JSON.stringify(body),
|
|
157
|
+
},
|
|
142
158
|
};
|
|
143
159
|
}
|
package/test/urls.js
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chai from 'chai';
|
|
2
|
+
import chaiHttp from 'chai-http';
|
|
3
|
+
import { serverStart, serverStop } from '../lib';
|
|
4
|
+
import sources from './sources';
|
|
5
|
+
|
|
6
|
+
chai.use(chaiHttp);
|
|
7
|
+
|
|
8
|
+
const should = chai.should();
|
|
9
|
+
|
|
10
|
+
describe('websocket API', () => {
|
|
11
|
+
let server;
|
|
12
|
+
|
|
13
|
+
before(() => {
|
|
14
|
+
server = serverStart(sources, { port: 1337, webSocketEnabled: true });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
after(() => {
|
|
18
|
+
serverStop();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should send message to clients', done => {
|
|
22
|
+
chai
|
|
23
|
+
.request(server)
|
|
24
|
+
.post('/api/v1/websocket/clients/message')
|
|
25
|
+
.end((err, res) => {
|
|
26
|
+
should.not.exist(err);
|
|
27
|
+
|
|
28
|
+
res.status.should.equal(200);
|
|
29
|
+
res.body.status.should.be.true;
|
|
30
|
+
done();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should disconnect all clients', done => {
|
|
35
|
+
chai
|
|
36
|
+
.request(server)
|
|
37
|
+
.post('/api/v1/websocket/clients/disconnect')
|
|
38
|
+
.end((err, res) => {
|
|
39
|
+
should.not.exist(err);
|
|
40
|
+
|
|
41
|
+
res.status.should.equal(200);
|
|
42
|
+
res.body.status.should.be.true;
|
|
43
|
+
done();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
});
|