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.
Files changed (49) 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/app.js +7 -3
  5. package/dist/app.js.map +1 -1
  6. package/dist/config.js +24 -22
  7. package/dist/config.js.map +1 -1
  8. package/dist/core/matchers/query-string-matcher.js +1 -1
  9. package/dist/core/matchers/query-string-matcher.js.map +1 -1
  10. package/dist/core/request-handler.js +5 -1
  11. package/dist/core/request-handler.js.map +1 -1
  12. package/dist/core/sources-parser.js +2 -1
  13. package/dist/core/sources-parser.js.map +1 -1
  14. package/dist/index.js +2 -2
  15. package/dist/index.js.map +1 -1
  16. package/package.json +4 -2
  17. package/.editorconfig +0 -13
  18. package/.prettierrc +0 -4
  19. package/dist/core/sources-parser.spec.js +0 -32
  20. package/dist/core/sources-parser.spec.js.map +0 -1
  21. package/lib/api/control.js +0 -35
  22. package/lib/api/recording/api-recorder.js +0 -47
  23. package/lib/api/recording/index.js +0 -23
  24. package/lib/api/websocket/index.js +0 -27
  25. package/lib/app.js +0 -72
  26. package/lib/config.js +0 -29
  27. package/lib/constants.js +0 -9
  28. package/lib/core/matchers/MatchItem.js +0 -11
  29. package/lib/core/matchers/MatchingResult.js +0 -80
  30. package/lib/core/matchers/http-get-matcher.js +0 -12
  31. package/lib/core/matchers/http-post-matcher.js +0 -124
  32. package/lib/core/matchers/query-string-matcher.js +0 -113
  33. package/lib/core/request-handler.js +0 -123
  34. package/lib/core/sources-parser.js +0 -199
  35. package/lib/core/sources-parser.spec.js +0 -31
  36. package/lib/core/sources-router.js +0 -97
  37. package/lib/core/utils.js +0 -7
  38. package/lib/index.js +0 -70
  39. package/src/index.js +0 -4
  40. package/test/Digger.js +0 -20
  41. package/test/control-api.spec.js +0 -105
  42. package/test/image.jpg +0 -0
  43. package/test/index.js +0 -183
  44. package/test/mock-api.spec.js +0 -46
  45. package/test/monitoring-api.spec.js +0 -236
  46. package/test/responsekey.js +0 -15
  47. package/test/sources.js +0 -159
  48. package/test/urls.js +0 -14
  49. package/test/websocket-api.spec.js +0 -47
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/index.js';
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('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;
package/test/sources.js DELETED
@@ -1,159 +0,0 @@
1
- import urls from './urls';
2
- import responseKey, { responseKeyParam } from './responsekey';
3
- import path from 'path';
4
-
5
- const q1q2q3FilledQs = [
6
- { name: 'q1', values: ['.*'] },
7
- { name: 'q2', values: ['.*'] },
8
- { name: 'q3', values: ['.*'] },
9
- ];
10
-
11
- const q1filledQs = [{ name: 'q1', values: ['1'] }];
12
-
13
- const dynamicResponseQs = [{ name: 'q1', values: ['.*'] }];
14
-
15
- const sources = [
16
- [
17
- // codes
18
- createSource({ method: 'GET', path: urls.base, statusCode: 200 }),
19
- createSource({ method: 'GET', path: urls.error, statusCode: 500 }),
20
- createSource({ method: 'POST', path: urls.error, statusCode: 500 }),
21
- createSource({ method: 'GET', path: urls.notFound, statusCode: 404 }),
22
- // query strings
23
- createSource({
24
- method: 'GET',
25
- path: urls.items,
26
- statusCode: 200,
27
- key: responseKey.qsOptionalEmpty,
28
- }),
29
- createSource({
30
- method: 'GET',
31
- path: urls.items,
32
- statusCode: 200,
33
- key: responseKey.qsOptionalQ1Q2Q3Filed,
34
- queryStringParameters: q1q2q3FilledQs,
35
- }),
36
- createSource({
37
- method: 'GET',
38
- path: urls.items,
39
- statusCode: 200,
40
- key: responseKey.qsOptionalQ1filled,
41
- queryStringParameters: q1filledQs,
42
- }),
43
- // item
44
- createSource({
45
- method: 'GET',
46
- path: urls.item,
47
- statusCode: 200,
48
- key: responseKey.urlParamBase,
49
- }),
50
- createSource({
51
- method: 'GET',
52
- path: urls.itemHardId,
53
- statusCode: 200,
54
- key: responseKey.urlParamHardId,
55
- }),
56
- createSource({
57
- method: 'GET',
58
- path: urls.items,
59
- statusCode: 200,
60
- key: responseKey.qsMultiQ1,
61
- queryStringParameters: [{ name: 'q1', values: ['1', '2', '3'] }],
62
- }),
63
-
64
- // dynamic response
65
-
66
- createDynamicSource({
67
- method: 'POST',
68
- path: urls.dynamicResponse,
69
- statusCode: 200,
70
- key: responseKey.dynamicResponse,
71
- queryStringParameters: dynamicResponseQs,
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
- ],
89
- ];
90
-
91
- export default sources;
92
-
93
- /**
94
- *
95
- */
96
- function createDynamicSource({
97
- method,
98
- path,
99
- statusCode,
100
- key,
101
- queryStringParameters = null,
102
- }) {
103
- let body = {};
104
- body[responseKeyParam] = key;
105
-
106
- return {
107
- request: {
108
- method,
109
- path,
110
- queryStringParameters,
111
- body: {
112
- type: 'JSON',
113
- value: JSON.stringify({
114
- bodyParam1: 'bodyValue1',
115
- }),
116
- matchType: 'ONLY_MATCHING_FIELDS',
117
- },
118
- },
119
- response: function (urlParams, qsParams, bodyParams) {
120
- body['requestParams'] = {
121
- qsParams,
122
- bodyParams,
123
- urlParams,
124
- };
125
- return {
126
- statusCode,
127
- body: JSON.stringify(body),
128
- };
129
- },
130
- };
131
- }
132
-
133
- /**
134
- *
135
- */
136
- export function createSource({
137
- method,
138
- path,
139
- statusCode,
140
- delay,
141
- key,
142
- queryStringParameters = null,
143
- }) {
144
- let body = {};
145
- body[responseKeyParam] = key;
146
-
147
- return {
148
- request: {
149
- method,
150
- path,
151
- queryStringParameters,
152
- },
153
- response: {
154
- delay,
155
- statusCode,
156
- body: JSON.stringify(body),
157
- },
158
- };
159
- }
package/test/urls.js DELETED
@@ -1,14 +0,0 @@
1
- const urls = {
2
- base: '/',
3
- error: '/error',
4
- notFound: '/not-found',
5
- items: '/items',
6
- itemsLogo: '/items/logo',
7
- item: '/item/:id',
8
- itemHardId: '/item/hardId',
9
- dynamicResponseBase: '/dynamic/response/',
10
- dynamicResponseKey: 'urlParam',
11
- dynamicResponse: '/dynamic/response/:urlParam'
12
- };
13
-
14
- export default urls;
@@ -1,47 +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('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
- });