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
@@ -1,199 +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.queryStringParameters = record.request.queryStringParameters;
10
- this.body = record.request.body;
11
- this.response = record.response;
12
- }
13
- }
14
-
15
- /**
16
- * Class for handling sources
17
- */
18
- export class SourcesParser {
19
- constructor(sources) {
20
- this.sources = sources;
21
- this.generateMapFromSources(sources);
22
- }
23
-
24
- /**
25
- * @public
26
- * @return {!Object}
27
- */
28
- getMap() {
29
- return this.map;
30
- }
31
-
32
- /**
33
- * @public
34
- */
35
- resetMap() {
36
- this.generateMapFromSources(this.sources);
37
- }
38
-
39
- /**
40
- * Method to initialize sources to map
41
- * @param {Object} sources
42
- */
43
- generateMapFromSources(sources) {
44
- const GENERATE_TITLE = 'Generate original map from sources';
45
- if (isLogEnabled) {
46
- console.time(GENERATE_TITLE);
47
- }
48
-
49
- this.map = {};
50
- sources.forEach((source) => {
51
- this.setSource(this.map, source);
52
- });
53
- this.sortMap();
54
-
55
- if (isLogEnabled) {
56
- console.timeEnd(GENERATE_TITLE);
57
- }
58
- }
59
-
60
- /**
61
- *
62
- */
63
- setSource(map, source) {
64
- source.forEach((record) => {
65
- this.setRecord(map, record);
66
- });
67
- }
68
-
69
- /**
70
- *
71
- */
72
- switchRecord(record) {
73
- const SET_NEW_REC = 'Set new record';
74
- if (isLogEnabled) console.time(SET_NEW_REC);
75
- let { method, path, queryStringParameters, body } = record.request;
76
- let isChanged = false;
77
- this.map[path][method].forEach((changedRecord, index) => {
78
- // console.log('**************'.blue);
79
- // console.log(JSON.stringify(body));
80
- // console.log(JSON.stringify(changedRecord.body));
81
- // console.log(JSON.stringify(body) == JSON.stringify(changedRecord.body));
82
- if (
83
- isQueryStringEqual(
84
- queryStringParameters,
85
- changedRecord.queryStringParameters
86
- ) &&
87
- JSON.stringify(body) === JSON.stringify(changedRecord.body)
88
- ) {
89
- // console.log('CHANGE FROM '.green);
90
- // console.log(JSON.stringify(this.map[path][method][index], null, " "));
91
- this.map[path][method][index] = new Record(record);
92
- // console.log('CHANGE TO '.blue);
93
- // console.log(JSON.stringify(this.map[path][method][index], null, " "));
94
- isChanged = true;
95
- }
96
- });
97
- if (!isChanged) {
98
- this.setRecord(this.map, record);
99
- }
100
- this.sortMap();
101
- if (isLogEnabled) console.timeEnd(SET_NEW_REC);
102
- }
103
-
104
- /**
105
- *
106
- */
107
- setRecord(map, record) {
108
- let { method, path } = record.request;
109
- if (!map[path]) map[path] = {};
110
- if (!map[path][method]) map[path][method] = [];
111
- map[path][method].push(new Record(record));
112
- }
113
-
114
- /**
115
- * Method to sort map
116
- */
117
- sortMap() {
118
- Object.keys(this.map).forEach((path) => {
119
- let pathItem = this.map[path];
120
- let gets = pathItem[methods.GET];
121
- if (gets) {
122
- gets.sort(compare);
123
- }
124
- let posts = pathItem[methods.POST];
125
- if (posts) {
126
- posts.sort(compare);
127
- }
128
- let puts = pathItem[methods.PUT];
129
- if (puts) {
130
- puts.sort(compare);
131
- }
132
- let deletes = pathItem[methods.DELETE];
133
- if (deletes) {
134
- deletes.sort(compare);
135
- }
136
- let patches = pathItem[methods.PATCH];
137
- if (patches) {
138
- patches.sort(compare);
139
- }
140
- });
141
- }
142
- }
143
-
144
- /**
145
- * TODO: refactoring
146
- */
147
- function isQueryStringEqual(qs1, qs2) {
148
- if ((qs1 && !qs2) || (!qs1 && qs2)) return false;
149
- if (!qs1 && !qs2) return true;
150
- if (qs1.length !== qs2.length) return false;
151
- let isEqual = true;
152
- let qs1ref = {};
153
- let qs2ref = {};
154
-
155
- qs1.forEach((item) => {
156
- qs1ref[item['name']] = item['values'];
157
- });
158
-
159
- qs2.forEach((item) => {
160
- qs2ref[item['name']] = item['values'];
161
- });
162
-
163
- Object.keys(qs1ref).forEach((name) => {
164
- let values = qs1ref[name];
165
- let qs2Values = qs2ref[name];
166
- if (qs2Values.length != values.length) {
167
- isEqual = false;
168
- } else {
169
- values.forEach((value) => {
170
- if (qs2Values.indexOf(value) === -1) {
171
- isEqual = false;
172
- }
173
- });
174
- }
175
- });
176
- return isEqual;
177
- }
178
-
179
- /**
180
- * Method to compare two requests for sorting by params count
181
- */
182
- function compare(a, b) {
183
- if (!a.queryStringParameters && !b.queryStringParameters) return 0;
184
- if (!a.queryStringParameters) return 1;
185
- if (!b.queryStringParameters) return -1;
186
- if (b.queryStringParameters.length != a.queryStringParameters.length)
187
- return b.queryStringParameters.length - a.queryStringParameters.length;
188
-
189
- // toto posledni pravidlo je kvuli obecnemu parametru, aby prevazili konkretni nad obecnym. 2 > .*
190
- let aSpecificCount = 0;
191
- let bSpecificCount = 0;
192
- a.queryStringParameters.forEach((qs, index) => {
193
- if (qs['values'].indexOf(REG_ALL) !== -1) aSpecificCount++;
194
- });
195
- b.queryStringParameters.forEach((qs) => {
196
- if (qs['values'].indexOf(REG_ALL) !== -1) bSpecificCount++;
197
- });
198
- return aSpecificCount - bSpecificCount;
199
- }
@@ -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
- }
package/lib/core/utils.js DELETED
@@ -1,7 +0,0 @@
1
- export const isFunction = (functionToCheck) => {
2
- const getType = {};
3
- return (
4
- functionToCheck &&
5
- getType.toString.call(functionToCheck) === '[object Function]'
6
- );
7
- };
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 = {}) {
20
- const app = new App(sources, serverConfig);
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