express-mock-server 3.4.3 → 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.
Files changed (41) hide show
  1. package/README.md +5 -0
  2. package/dist/api/recording/api-recorder.js +12 -1
  3. package/dist/api/recording/api-recorder.js.map +1 -1
  4. package/dist/config.js +24 -22
  5. package/dist/config.js.map +1 -1
  6. package/dist/core/matchers/query-string-matcher.js +1 -1
  7. package/dist/core/matchers/query-string-matcher.js.map +1 -1
  8. package/package.json +4 -2
  9. package/.editorconfig +0 -13
  10. package/.prettierrc +0 -4
  11. package/dist/core/sources-parser.spec.js +0 -32
  12. package/dist/core/sources-parser.spec.js.map +0 -1
  13. package/lib/api/control.js +0 -35
  14. package/lib/api/recording/api-recorder.js +0 -47
  15. package/lib/api/recording/index.js +0 -23
  16. package/lib/api/websocket/index.js +0 -27
  17. package/lib/app.js +0 -76
  18. package/lib/config.js +0 -29
  19. package/lib/constants.js +0 -9
  20. package/lib/core/matchers/MatchItem.js +0 -11
  21. package/lib/core/matchers/MatchingResult.js +0 -80
  22. package/lib/core/matchers/http-get-matcher.js +0 -12
  23. package/lib/core/matchers/http-post-matcher.js +0 -124
  24. package/lib/core/matchers/query-string-matcher.js +0 -113
  25. package/lib/core/request-handler.js +0 -129
  26. package/lib/core/sources-parser.js +0 -200
  27. package/lib/core/sources-parser.spec.js +0 -31
  28. package/lib/core/sources-router.js +0 -97
  29. package/lib/core/utils.js +0 -7
  30. package/lib/index.js +0 -70
  31. package/src/index.js +0 -4
  32. package/test/Digger.js +0 -20
  33. package/test/control-api.spec.js +0 -105
  34. package/test/image.jpg +0 -0
  35. package/test/index.js +0 -183
  36. package/test/mock-api.spec.js +0 -46
  37. package/test/monitoring-api.spec.js +0 -236
  38. package/test/responsekey.js +0 -15
  39. package/test/sources.js +0 -174
  40. package/test/urls.js +0 -14
  41. package/test/websocket-api.spec.js +0 -47
package/lib/index.js DELETED
@@ -1,70 +0,0 @@
1
- import 'colors';
2
- import config from './config';
3
- import { App } from './app';
4
- import ws from 'ws';
5
-
6
- const { port } = config;
7
- let server,
8
- wsServer = null;
9
-
10
- /**
11
- * Method to start server.
12
- *
13
- * @param sources
14
- * @param {Object} serverConfig
15
- * @param serverConfig.controlApiUrl ['/api/v1']
16
- * @param serverConfig.port [8080]
17
- * @param serverConfig.webSocketEnabled false
18
- */
19
- export function serverStart(sources, serverConfig = {}, customMiddlewareFn) {
20
- const app = new App(sources, serverConfig, customMiddlewareFn);
21
- const port = getServerPort(serverConfig);
22
- server = app.start(port, () => {
23
- console.log(`* Server port ${port}`.yellow);
24
- console.log(`*************************`.yellow);
25
- console.log('Server START'.bgGreen.white);
26
- });
27
-
28
- if (serverConfig.webSocketEnabled) {
29
- wsServer = startWebSocket(server);
30
- app.initWebSocketApi(wsServer);
31
- }
32
-
33
- return server;
34
- }
35
-
36
- /**
37
- * Method to stop server.
38
- */
39
- export function serverStop() {
40
- if (server) {
41
- server.close();
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;
66
- }
67
-
68
- function getServerPort(serverConfig) {
69
- return serverConfig.port ? serverConfig.port : port;
70
- }
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
- import { serverStart } from '../lib';
2
- import sources from '../test/sources';
3
-
4
- serverStart(sources, { port: 2121, webSocketEnabled: true });
package/test/Digger.js DELETED
@@ -1,20 +0,0 @@
1
- class Digger {
2
- constructor(res, onResponse) {
3
- this.data = '';
4
- this.onResponse = onResponse;
5
- res.on('data', chunk => {
6
- this.data += chunk;
7
- });
8
- res.on('end', this.makeOnResponse.bind(this));
9
- }
10
-
11
- makeOnResponse() {
12
- this.onResponse(this.getData());
13
- }
14
-
15
- getData() {
16
- return JSON.parse(this.data);
17
- }
18
- }
19
-
20
- export default Digger;
@@ -1,105 +0,0 @@
1
- import chai from 'chai';
2
- import chaiHttp from 'chai-http';
3
- import { serverStart, serverStop } from '../lib';
4
- import urls from './urls';
5
- import { createSource } from './sources';
6
-
7
- chai.use(chaiHttp);
8
-
9
- const should = chai.should();
10
-
11
- const sources = [
12
- [
13
- createSource({
14
- method: 'GET',
15
- path: urls.items,
16
- statusCode: 200,
17
- key: [{ id: 1 }]
18
- })
19
- ]
20
- ];
21
-
22
- const sourceSwitch = [
23
- createSource({
24
- method: 'GET',
25
- path: urls.items,
26
- statusCode: 400,
27
- key: { message: 'API set' }
28
- })
29
- ];
30
-
31
- describe('control API', () => {
32
- let server;
33
-
34
- before(() => {
35
- server = serverStart(sources, { port: 1337 });
36
- });
37
-
38
- after(() => {
39
- serverStop();
40
- });
41
-
42
- it('should set new response for API', done => {
43
- chai
44
- .request(server)
45
- .get('/items')
46
- .then(res => {
47
- res.status.should.equal(200);
48
- res.body.responseKey.should.be.a('array').to.have.lengthOf(1);
49
-
50
- return chai
51
- .request(server)
52
- .post('/api/v1/set')
53
- .send(sourceSwitch);
54
- })
55
- .then(res => {
56
- res.status.should.equal(202);
57
- return chai.request(server).get('/items');
58
- })
59
- .then(res => {
60
- res.status.should.equal(400);
61
- res.body.responseKey.message.should.equal('API set');
62
- done();
63
- });
64
- });
65
-
66
- it('should reset response for API', done => {
67
- chai
68
- .request(server)
69
- .post('/api/v1/set')
70
- .send(sourceSwitch)
71
- .end((err, res) => {
72
- should.not.exist(err);
73
- res.status.should.equal(202);
74
-
75
- chai
76
- .request(server)
77
- .get('/items')
78
- .end((err, res) => {
79
- should.not.exist(err);
80
- res.status.should.equal(400);
81
- res.body.responseKey.message.should.equal('API set');
82
-
83
- chai
84
- .request(server)
85
- .post('/api/v1/reset')
86
- .end((err, res) => {
87
- should.not.exist(err);
88
- res.status.should.equal(202);
89
-
90
- chai
91
- .request(server)
92
- .get('/items')
93
- .end((err, res) => {
94
- should.not.exist(err);
95
- res.status.should.equal(200);
96
- res.body.responseKey.should.be
97
- .a('array')
98
- .to.have.lengthOf(1);
99
- done();
100
- });
101
- });
102
- });
103
- });
104
- });
105
- });
package/test/image.jpg DELETED
Binary file
package/test/index.js DELETED
@@ -1,183 +0,0 @@
1
- import assert from 'assert';
2
- import http from 'http';
3
- import request from 'request';
4
- import { serverStart, serverStop } from '../lib';
5
- import sources from './sources';
6
- import urls from './urls';
7
- import responseKey, { responseKeyParam } from './responsekey';
8
- import Digger from './Digger';
9
- import { client as WebSocketClient } from 'websocket';
10
-
11
- const PORT = 1337;
12
- const SERVER_NAME = '127.0.0.1';
13
- const SERVER_URL_WITHOUT_PORT = 'http://' + SERVER_NAME;
14
- const SERVER_URL = SERVER_URL_WITHOUT_PORT + ':' + PORT;
15
-
16
- const WS_SERVER_URL = 'ws://' + SERVER_NAME + ':' + PORT;
17
-
18
- function createItemsExpectation(qs, key, done) {
19
- http.get(SERVER_URL + urls.items + qs, (res) => {
20
- new Digger(res, (data) => {
21
- assert.equal(key, data[responseKeyParam]);
22
- done();
23
- });
24
- });
25
- }
26
-
27
- function createItemExpectation(id, key, done) {
28
- http.get(SERVER_URL + urls.item + id, (res) => {
29
- new Digger(res, (data) => {
30
- assert.equal(key, data[responseKeyParam]);
31
- done();
32
- });
33
- });
34
- }
35
-
36
- function createCodeExpectation(url, code, done) {
37
- http.get(SERVER_URL + url, (res) => {
38
- assert.equal(code, res.statusCode);
39
- done();
40
- });
41
- }
42
-
43
- describe('server', () => {
44
- before(() => {
45
- serverStart(sources, { port: PORT, webSocketEnabled: true });
46
- });
47
-
48
- after(() => {
49
- serverStop();
50
- });
51
-
52
- describe('Basic working', () => {
53
- it('should return 200', (done) => {
54
- createCodeExpectation('', 200, done);
55
- });
56
-
57
- it('should return 500', (done) => {
58
- createCodeExpectation(urls.error, 500, done);
59
- });
60
-
61
- it('should return 404', (done) => {
62
- createCodeExpectation(urls.notFound, 404, done);
63
- });
64
-
65
- // it('data should be 21', (done) => {
66
- // http.get(SERVER_URL, res => {
67
- // new Digger(res, (data) => {
68
- // assert.equal(21, data.data);
69
- // done();
70
- // });
71
- // });
72
- // });
73
- });
74
-
75
- describe('Query strings', () => {
76
- it('no query = expected empty', (done) => {
77
- createItemsExpectation('', responseKey.qsOptionalEmpty, done);
78
- });
79
-
80
- it('only q1 filled = expected q1', (done) => {
81
- const qs = '?q1=1';
82
- createItemsExpectation(qs, responseKey.qsOptionalQ1filled, done);
83
- });
84
-
85
- it('only q1 wrong filled = expected empty', (done) => {
86
- const qs = '?q1=2';
87
- createItemsExpectation(qs, responseKey.qsOptionalEmpty, done);
88
- });
89
-
90
- it('multi q1 filled = matched for the same values', (done) => {
91
- const qs = '?q1=1&q1=2&q1=3';
92
- createItemsExpectation(qs, responseKey.qsMultiQ1, done);
93
- });
94
-
95
- it('multi q1 filled = matched also for different order of values', (done) => {
96
- const qs = '?q1=1&q1=3&q1=2';
97
- createItemsExpectation(qs, responseKey.qsMultiQ1, done);
98
- });
99
-
100
- it('multi q1 filled = no matched because more values', (done) => {
101
- const qs = '?q1=1&q1=2&q1=3&q1=4';
102
- createItemsExpectation(qs, responseKey.qsOptionalEmpty, done);
103
- });
104
-
105
- it('multi q1 filled = no matched because one value is different', (done) => {
106
- const qs = '?q1=1&q1=2&q1=4';
107
- createItemsExpectation(qs, responseKey.qsOptionalEmpty, done);
108
- });
109
-
110
- it('multi q1 filled = no matched because less values', (done) => {
111
- const qs = '?q1=1&q1=2';
112
- createItemsExpectation(qs, responseKey.qsOptionalEmpty, done);
113
- });
114
-
115
- it('q1, q2, q3 filled = expected q1, q2, q3', (done) => {
116
- const qs = '?q1=1&q2=2&q3=3';
117
- createItemsExpectation(qs, responseKey.qsOptionalQ1Q2Q3Filed, done);
118
- });
119
-
120
- it('q1, q2 filled = expected q1', (done) => {
121
- const qs = '?q1=1&q2=2';
122
- createItemsExpectation(qs, responseKey.qsOptionalQ1filled, done);
123
- });
124
- });
125
-
126
- describe('url params', () => {
127
- it('item id lolek1 = expected urlParamBase', (done) => {
128
- createItemExpectation('lolek1', responseKey.urlParamBase, done);
129
- });
130
-
131
- // it('item id hardId = expected urlParamHardId', (done) => {
132
- // createItemExpectation('hardId', responseKey.urlParamHardId, done);
133
- // });
134
- });
135
-
136
- describe('dynamic response', () => {
137
- it('query should be in response', (done) => {
138
- createDynamicResponseExpectation(responseKey.dynamicResponse, done);
139
- });
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
-
153
- function createDynamicResponseExpectation(key, done) {
154
- const urlParamValue = 'urlParamValue';
155
-
156
- const postKey = 'bodyParam1';
157
- const postValue = 'bodyValue1';
158
- let postData = {};
159
- postData[postKey] = postValue;
160
-
161
- const qsKey = 'q1';
162
- const qsValue = 'lolek1';
163
- var qs = '?' + qsKey + '=' + qsValue;
164
-
165
- var options = {
166
- method: 'post',
167
- body: postData, // Javascript object
168
- json: true, // Use,If you are sending JSON data
169
- url: SERVER_URL + urls.dynamicResponseBase + urlParamValue + qs,
170
- };
171
-
172
- request(options, (err, res, body) => {
173
- assert.equal(key, body[responseKeyParam]);
174
- assert.equal(
175
- urlParamValue,
176
- body.requestParams.urlParams[urls.dynamicResponseKey]
177
- );
178
- assert.equal(qsValue, body.requestParams.qsParams[qsKey]);
179
- assert.equal(postValue, body.requestParams.bodyParams[postKey]);
180
- done();
181
- });
182
- }
183
- });
@@ -1,46 +0,0 @@
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.only('API mocks', () => {
11
- let server;
12
-
13
- before(() => {
14
- server = serverStart(sources, { port: 1337 });
15
- });
16
-
17
- after(() => {
18
- serverStop();
19
- });
20
-
21
- it('should call mocked API', done => {
22
- chai
23
- .request(server)
24
- .get('/item/5')
25
- .end((err, res) => {
26
- should.not.exist(err);
27
- res.status.should.equal(200);
28
- res.body.responseKey.should.equal('urlParamBase');
29
- done();
30
- });
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
- });
46
- });
@@ -1,236 +0,0 @@
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('recording API', () => {
11
- let server;
12
-
13
- before(() => {
14
- server = serverStart(sources, { port: 1337 });
15
- });
16
-
17
- after(() => {
18
- serverStop();
19
- });
20
-
21
- it('should start monitoring', done => {
22
- chai
23
- .request(server)
24
- .post('/api/v1/recording/start')
25
- .end((err, res) => {
26
- should.not.exist(err);
27
- res.status.should.equal(200);
28
- res.body.id.should.be.a('string');
29
- done();
30
- });
31
- });
32
-
33
- it('should return empty monitoring result', done => {
34
- chai
35
- .request(server)
36
- .post('/api/v1/recording/start')
37
- .end((err, res) => {
38
- should.not.exist(err);
39
- res.status.should.equal(200);
40
- res.body.id.should.be.a('string');
41
-
42
- const monitoringId = res.body.id;
43
-
44
- chai
45
- .request(server)
46
- .post(`/api/v1/recording/${monitoringId}/stop`)
47
- .end((err, res) => {
48
- should.not.exist(err);
49
- res.status.should.equal(200);
50
- res.body.should.be.an('array').and.be.empty;
51
- done();
52
- });
53
- });
54
- });
55
-
56
- it('should return 404 if monitoring ID does not exists', done => {
57
- chai
58
- .request(server)
59
- .post('/api/v1/recording/xxx/stop')
60
- .end((err, res) => {
61
- res.status.should.equal(404);
62
- done();
63
- });
64
- });
65
-
66
- it('should monitor GET API call', done => {
67
- chai
68
- .request(server)
69
- .post('/api/v1/recording/start')
70
- .end((err, res) => {
71
- should.not.exist(err);
72
- res.status.should.equal(200);
73
- res.body.id.should.be.a('string');
74
-
75
- const monitoringId = res.body.id;
76
-
77
- chai
78
- .request(server)
79
- .get(`/items`)
80
- .end((err, res) => {
81
- should.not.exist(err);
82
- res.status.should.equal(200);
83
-
84
- chai
85
- .request(server)
86
- .post(`/api/v1/recording/${monitoringId}/stop`)
87
- .end((err, res) => {
88
- should.not.exist(err);
89
- res.status.should.equal(200);
90
- res.body.should.be.an('array').and.to.have.lengthOf(1);
91
- res.body[0].method.should.equal('GET');
92
- res.body[0].path.should.equal('/items');
93
- done();
94
- });
95
- });
96
- });
97
- });
98
-
99
- it('should monitor POST API call with empty body', done => {
100
- chai
101
- .request(server)
102
- .post('/api/v1/recording/start')
103
- .end((err, res) => {
104
- should.not.exist(err);
105
- res.status.should.equal(200);
106
- res.body.id.should.be.a('string');
107
-
108
- const monitoringId = res.body.id;
109
-
110
- chai
111
- .request(server)
112
- .post(`/dynamic/response/555?queryParam=queryValue`)
113
- .end((err, res) => {
114
- should.not.exist(err);
115
- res.status.should.equal(200);
116
-
117
- chai
118
- .request(server)
119
- .post(`/api/v1/recording/${monitoringId}/stop`)
120
- .end((err, res) => {
121
- should.not.exist(err);
122
- res.status.should.equal(200);
123
- res.body.should.be.an('array').and.to.have.lengthOf(1);
124
- res.body[0].method.should.equal('POST');
125
- res.body[0].path.should.equal('/dynamic/response/555');
126
- res.body[0].query.should.deep.equal({
127
- queryParam: 'queryValue'
128
- });
129
-
130
- res.body[0].responseBody.should.deep.equal({
131
- responseKey: 'dynamicResponse',
132
- requestParams: {
133
- qsParams: { queryParam: 'queryValue' },
134
- bodyParams: {},
135
- urlParams: { urlParam: '555' }
136
- }
137
- });
138
- done();
139
- });
140
- });
141
- });
142
- });
143
-
144
- it('should monitor POST API call with application/json', done => {
145
- chai
146
- .request(server)
147
- .post('/api/v1/recording/start')
148
- .end((err, res) => {
149
- should.not.exist(err);
150
- res.status.should.equal(200);
151
- res.body.id.should.be.a('string');
152
-
153
- const monitoringId = res.body.id;
154
-
155
- chai
156
- .request(server)
157
- .post(`/dynamic/response/555?queryParam=queryValue`)
158
- .send({ test: 'pokus' })
159
- .end((err, res) => {
160
- should.not.exist(err);
161
- res.status.should.equal(200);
162
-
163
- chai
164
- .request(server)
165
- .post(`/api/v1/recording/${monitoringId}/stop`)
166
- .end((err, res) => {
167
- should.not.exist(err);
168
- res.status.should.equal(200);
169
- res.body.should.be.an('array').and.to.have.lengthOf(1);
170
- res.body[0].method.should.equal('POST');
171
- res.body[0].path.should.equal('/dynamic/response/555');
172
- res.body[0].query.should.deep.equal({
173
- queryParam: 'queryValue'
174
- });
175
-
176
- res.body[0].responseBody.should.deep.equal({
177
- responseKey: 'dynamicResponse',
178
- requestParams: {
179
- qsParams: { queryParam: 'queryValue' },
180
- bodyParams: { test: 'pokus' },
181
- urlParams: { urlParam: '555' }
182
- }
183
- });
184
- done();
185
- });
186
- });
187
- });
188
- });
189
-
190
- it('should monitor POST API call with text/plain', done => {
191
- chai
192
- .request(server)
193
- .post('/api/v1/recording/start')
194
- .end((err, res) => {
195
- should.not.exist(err);
196
- res.status.should.equal(200);
197
- res.body.id.should.be.a('string');
198
-
199
- const monitoringId = res.body.id;
200
-
201
- chai
202
- .request(server)
203
- .post(`/dynamic/response/555?queryParam=queryValue`)
204
- .set('content-type', 'text/plain')
205
- .send('test text body')
206
- .end((err, res) => {
207
- should.not.exist(err);
208
- res.status.should.equal(200);
209
-
210
- chai
211
- .request(server)
212
- .post(`/api/v1/recording/${monitoringId}/stop`)
213
- .end((err, res) => {
214
- should.not.exist(err);
215
- res.status.should.equal(200);
216
- res.body.should.be.an('array').and.to.have.lengthOf(1);
217
- res.body[0].method.should.equal('POST');
218
- res.body[0].path.should.equal('/dynamic/response/555');
219
- res.body[0].query.should.deep.equal({
220
- queryParam: 'queryValue'
221
- });
222
-
223
- res.body[0].responseBody.should.deep.equal({
224
- responseKey: 'dynamicResponse',
225
- requestParams: {
226
- qsParams: { queryParam: 'queryValue' },
227
- bodyParams: 'test text body',
228
- urlParams: { urlParam: '555' }
229
- }
230
- });
231
- done();
232
- });
233
- });
234
- });
235
- });
236
- });
@@ -1,15 +0,0 @@
1
- export const responseKeyParam = 'responseKey';
2
-
3
- const responseKey = {
4
- qsOptionalEmpty: 'qsOptionalEmpty',
5
- qsMultiQ1: 'qsMultiQ1',
6
- qsOptionalQ1Q2Q3Filed: 'qsOptionalQ1Q2Q3Filed',
7
- qsOptionalQ1filled: 'qsOptionalQ1filled',
8
-
9
- urlParamBase: 'urlParamBase',
10
- urlParamHardId: 'urlParamHardId',
11
-
12
- dynamicResponse: 'dynamicResponse'
13
- };
14
-
15
- export default responseKey;