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.
Files changed (51) hide show
  1. package/dist/api/control.js +2 -2
  2. package/dist/api/control.js.map +1 -1
  3. package/dist/api/recording/api-recorder.js +3 -5
  4. package/dist/api/recording/api-recorder.js.map +1 -1
  5. package/dist/api/recording/index.js.map +1 -1
  6. package/dist/api/websocket/index.js +35 -0
  7. package/dist/api/websocket/index.js.map +1 -0
  8. package/dist/app.js +10 -0
  9. package/dist/app.js.map +1 -1
  10. package/dist/config.js.map +1 -1
  11. package/dist/constants.js +1 -1
  12. package/dist/constants.js.map +1 -1
  13. package/dist/core/matchers/MatchItem.js.map +1 -1
  14. package/dist/core/matchers/MatchingResult.js +1 -1
  15. package/dist/core/matchers/MatchingResult.js.map +1 -1
  16. package/dist/core/matchers/http-post-matcher.js.map +1 -1
  17. package/dist/core/matchers/query-string-matcher.js +3 -3
  18. package/dist/core/matchers/query-string-matcher.js.map +1 -1
  19. package/dist/core/request-handler.js +42 -6
  20. package/dist/core/request-handler.js.map +1 -1
  21. package/dist/core/sources-parser.js.map +1 -1
  22. package/dist/core/sources-parser.spec.js.map +1 -1
  23. package/dist/core/sources-router.js +11 -11
  24. package/dist/core/sources-router.js.map +1 -1
  25. package/dist/core/utils.js.map +1 -1
  26. package/dist/index.js +35 -1
  27. package/dist/index.js.map +1 -1
  28. package/lib/api/control.js +5 -5
  29. package/lib/api/recording/api-recorder.js +6 -6
  30. package/lib/api/recording/index.js +1 -1
  31. package/lib/api/websocket/index.js +27 -0
  32. package/lib/app.js +9 -0
  33. package/lib/config.js +1 -1
  34. package/lib/constants.js +1 -1
  35. package/lib/core/matchers/MatchingResult.js +1 -1
  36. package/lib/core/matchers/http-post-matcher.js +1 -1
  37. package/lib/core/matchers/query-string-matcher.js +4 -4
  38. package/lib/core/request-handler.js +51 -8
  39. package/lib/core/sources-parser.js +8 -8
  40. package/lib/core/sources-parser.spec.js +6 -6
  41. package/lib/core/sources-router.js +12 -12
  42. package/lib/core/utils.js +1 -1
  43. package/lib/index.js +33 -1
  44. package/package.json +17 -15
  45. package/src/index.js +1 -1
  46. package/test/image.jpg +0 -0
  47. package/test/index.js +15 -3
  48. package/test/mock-api.spec.js +14 -0
  49. package/test/sources.js +37 -21
  50. package/test/urls.js +1 -0
  51. package/test/websocket-api.spec.js +47 -0
@@ -34,8 +34,8 @@ class SourcesRouter {
34
34
 
35
35
  if (gets) {
36
36
  gets.forEach(() => {
37
- app.get(path, (req, res) => {
38
- (0, _requestHandler.requestHandler)(this.parser, req, res);
37
+ app.get(path, (req, res, next) => {
38
+ (0, _requestHandler.requestHandler)(this.parser, req, res, next);
39
39
  });
40
40
  this.logRegisteredPath(isLogEnabled, _constants.methods.GET, path);
41
41
  });
@@ -48,8 +48,8 @@ class SourcesRouter {
48
48
 
49
49
  if (posts) {
50
50
  posts.forEach(() => {
51
- app.post(path, (req, res) => {
52
- (0, _requestHandler.requestHandler)(this.parser, req, res);
51
+ app.post(path, (req, res, next) => {
52
+ (0, _requestHandler.requestHandler)(this.parser, req, res, next);
53
53
  });
54
54
  this.logRegisteredPath(isLogEnabled, _constants.methods.POST, path);
55
55
  });
@@ -62,8 +62,8 @@ class SourcesRouter {
62
62
 
63
63
  if (puts) {
64
64
  puts.forEach(() => {
65
- app.put(path, (req, res) => {
66
- (0, _requestHandler.requestHandler)(this.parser, req, res);
65
+ app.put(path, (req, res, next) => {
66
+ (0, _requestHandler.requestHandler)(this.parser, req, res, next);
67
67
  });
68
68
  this.logRegisteredPath(isLogEnabled, _constants.methods.PUT, path);
69
69
  });
@@ -76,8 +76,8 @@ class SourcesRouter {
76
76
 
77
77
  if (deletes) {
78
78
  deletes.forEach(() => {
79
- app.delete(path, (req, res) => {
80
- (0, _requestHandler.requestHandler)(this.parser, req, res);
79
+ app.delete(path, (req, res, next) => {
80
+ (0, _requestHandler.requestHandler)(this.parser, req, res, next);
81
81
  });
82
82
  this.logRegisteredPath(isLogEnabled, _constants.methods.DELETE, path);
83
83
  });
@@ -90,15 +90,15 @@ class SourcesRouter {
90
90
 
91
91
  if (patches) {
92
92
  patches.forEach(() => {
93
- app.patch(path, (req, res) => {
94
- (0, _requestHandler.requestHandler)(this.parser, req, res);
93
+ app.patch(path, (req, res, next) => {
94
+ (0, _requestHandler.requestHandler)(this.parser, req, res, next);
95
95
  });
96
96
  this.logRegisteredPath(isLogEnabled, _constants.methods.PATCH, path);
97
97
  });
98
98
  }
99
99
  }
100
100
  /**
101
- * Method to log registred path
101
+ * Method to log registered path
102
102
  */
103
103
 
104
104
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/core/sources-router.js"],"names":["SourcesRouter","constructor","parser","registerSources","app","isLogEnabled","map","getMap","Object","keys","forEach","path","registerGET","registerPOST","registerPUT","registerDELETE","registerPATCH","pathItem","gets","methods","GET","get","req","res","logRegisteredPath","posts","POST","post","puts","PUT","put","deletes","DELETE","delete","patches","PATCH","patch","method","console","log","magenta"],"mappings":";;;;;;;AAAA;;AACA;;AAEO,MAAMA,aAAN,CAAoB;AACzB;;;AAGAC,EAAAA,WAAW,CAACC,MAAD,EAAS;AAClB,SAAKA,MAAL,GAAcA,MAAd;AACD;;AAEDC,EAAAA,eAAe,CAACC,GAAD,EAAMC,YAAN,EAAoB;AACjC,UAAMC,GAAG,GAAG,KAAKJ,MAAL,CAAYK,MAAZ,EAAZ;AACAC,IAAAA,MAAM,CAACC,IAAP,CAAYH,GAAZ,EAAiBI,OAAjB,CAAyBC,IAAI,IAAI;AAC/B,WAAKC,WAAL,CAAiBR,GAAjB,EAAsBO,IAAtB,EAA4BN,YAA5B;AACA,WAAKQ,YAAL,CAAkBT,GAAlB,EAAuBO,IAAvB,EAA6BN,YAA7B;AACA,WAAKS,WAAL,CAAiBV,GAAjB,EAAsBO,IAAtB,EAA4BN,YAA5B;AACA,WAAKU,cAAL,CAAoBX,GAApB,EAAyBO,IAAzB,EAA+BN,YAA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,QAAQ,CAACE,mBAAQgB,KAAT,CAAxB;;AACA,QAAID,OAAJ,EAAa;AACXA,MAAAA,OAAO,CAACxB,OAAR,CAAgB,MAAM;AACpBN,QAAAA,GAAG,CAACgC,KAAJ,CAAUzB,IAAV,EAAgB,CAACW,GAAD,EAAMC,GAAN,KAAc;AAC5B,8CAAe,KAAKrB,MAApB,EAA4BoB,GAA5B,EAAiCC,GAAjC;AACD,SAFD;AAGA,aAAKC,iBAAL,CAAuBnB,YAAvB,EAAqCc,mBAAQgB,KAA7C,EAAoDxB,IAApD;AACD,OALD;AAMD;AACF;AAED;;;;;AAGAa,EAAAA,iBAAiB,CAACnB,YAAD,EAAegC,MAAf,EAAuB1B,IAAvB,EAA6B;AAC5C,QAAIN,YAAJ,EAAkB;AAChBiC,MAAAA,OAAO,CAACC,GAAR,CAAa,OAAMF,MAAO,IAAG1B,IAAK,EAAtB,CAAwB6B,OAApC;AACD;AACF;;AA5FwB","sourcesContent":["import { methods } from '../constants';\nimport { requestHandler } from './request-handler';\n\nexport class SourcesRouter {\n /**\n * @param {SourcesParser} parser\n */\n constructor(parser) {\n this.parser = parser;\n }\n\n registerSources(app, isLogEnabled) {\n const map = this.parser.getMap();\n Object.keys(map).forEach(path => {\n this.registerGET(app, path, isLogEnabled);\n this.registerPOST(app, path, isLogEnabled);\n this.registerPUT(app, path, isLogEnabled);\n this.registerDELETE(app, path, isLogEnabled);\n this.registerPATCH(app, path, isLogEnabled);\n });\n }\n\n registerGET(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const gets = pathItem[methods.GET];\n\n if (gets) {\n gets.forEach(() => {\n app.get(path, (req, res) => {\n requestHandler(this.parser, req, res);\n });\n this.logRegisteredPath(isLogEnabled, methods.GET, path);\n });\n }\n }\n\n registerPOST(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const posts = pathItem[methods.POST];\n if (posts) {\n posts.forEach(() => {\n app.post(path, (req, res) => {\n requestHandler(this.parser, req, res);\n });\n this.logRegisteredPath(isLogEnabled, methods.POST, path);\n });\n }\n }\n\n registerPUT(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const puts = pathItem[methods.PUT];\n if (puts) {\n puts.forEach(() => {\n app.put(path, (req, res) => {\n requestHandler(this.parser, req, res);\n });\n this.logRegisteredPath(isLogEnabled, methods.PUT, path);\n });\n }\n }\n\n registerDELETE(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const deletes = pathItem[methods.DELETE];\n if (deletes) {\n deletes.forEach(() => {\n app.delete(path, (req, res) => {\n requestHandler(this.parser, req, res);\n });\n this.logRegisteredPath(isLogEnabled, methods.DELETE, path);\n });\n }\n }\n\n registerPATCH(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const patches = pathItem[methods.PATCH];\n if (patches) {\n patches.forEach(() => {\n app.patch(path, (req, res) => {\n requestHandler(this.parser, req, res);\n });\n this.logRegisteredPath(isLogEnabled, methods.PATCH, path);\n });\n }\n }\n\n /**\n * Method to log registred path\n */\n logRegisteredPath(isLogEnabled, method, path) {\n if (isLogEnabled) {\n console.log(`REG ${method} ${path}`.magenta);\n }\n }\n}\n"],"file":"sources-router.js"}
1
+ {"version":3,"sources":["../../lib/core/sources-router.js"],"names":["SourcesRouter","constructor","parser","registerSources","app","isLogEnabled","map","getMap","Object","keys","forEach","path","registerGET","registerPOST","registerPUT","registerDELETE","registerPATCH","pathItem","gets","methods","GET","get","req","res","next","logRegisteredPath","posts","POST","post","puts","PUT","put","deletes","DELETE","delete","patches","PATCH","patch","method","console","log","magenta"],"mappings":";;;;;;;AAAA;;AACA;;AAEO,MAAMA,aAAN,CAAoB;AACzB;AACF;AACA;AACEC,EAAAA,WAAW,CAACC,MAAD,EAAS;AAClB,SAAKA,MAAL,GAAcA,MAAd;AACD;;AAEDC,EAAAA,eAAe,CAACC,GAAD,EAAMC,YAAN,EAAoB;AACjC,UAAMC,GAAG,GAAG,KAAKJ,MAAL,CAA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M;AACpBN,QAAAA,GAAG,CAAC8B,MAAJ,CAAWvB,IAAX,EAAiB,CAACW,GAAD,EAAMC,GAAN,EAAWC,IAAX,KAAoB;AACnC,8CAAe,KAAKtB,MAApB,EAA4BoB,GAA5B,EAAiCC,GAAjC,EAAsCC,IAAtC;AACD,SAFD;AAGA,aAAKC,iBAAL,CAAuBpB,YAAvB,EAAqCc,mBAAQc,MAA7C,EAAqDtB,IAArD;AACD,OALD;AAMD;AACF;;AAEDK,EAAAA,aAAa,CAACZ,GAAD,EAAMO,IAAN,EAAYL,GAAZ,EAAiBD,YAAjB,EAA+B;AAC1C,UAAMY,QAAQ,GAAG,KAAKf,MAAL,CAAYK,MAAZ,GAAqBI,IAArB,CAAjB;AACA,UAAMwB,OAAO,GAAGlB,QAAQ,CAACE,mBAAQiB,KAAT,CAAxB;;AACA,QAAID,OAAJ,EAAa;AACXA,MAAAA,OAAO,CAACzB,OAAR,CAAgB,MAAM;AACpBN,QAAAA,GAAG,CAACiC,KAAJ,CAAU1B,IAAV,EAAgB,CAACW,GAAD,EAAMC,GAAN,EAAWC,IAAX,KAAoB;AAClC,8CAAe,KAAKtB,MAApB,EAA4BoB,GAA5B,EAAiCC,GAAjC,EAAsCC,IAAtC;AACD,SAFD;AAGA,aAAKC,iBAAL,CAAuBpB,YAAvB,EAAqCc,mBAAQiB,KAA7C,EAAoDzB,IAApD;AACD,OALD;AAMD;AACF;AAED;AACF;AACA;;;AACEc,EAAAA,iBAAiB,CAACpB,YAAD,EAAeiC,MAAf,EAAuB3B,IAAvB,EAA6B;AAC5C,QAAIN,YAAJ,EAAkB;AAChBkC,MAAAA,OAAO,CAACC,GAAR,CAAa,OAAMF,MAAO,IAAG3B,IAAK,EAAtB,CAAwB8B,OAApC;AACD;AACF;;AA5FwB","sourcesContent":["import { methods } from '../constants';\nimport { requestHandler } from './request-handler';\n\nexport class SourcesRouter {\n /**\n * @param {SourcesParser} parser\n */\n constructor(parser) {\n this.parser = parser;\n }\n\n registerSources(app, isLogEnabled) {\n const map = this.parser.getMap();\n Object.keys(map).forEach((path) => {\n this.registerGET(app, path, isLogEnabled);\n this.registerPOST(app, path, isLogEnabled);\n this.registerPUT(app, path, isLogEnabled);\n this.registerDELETE(app, path, isLogEnabled);\n this.registerPATCH(app, path, isLogEnabled);\n });\n }\n\n registerGET(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const gets = pathItem[methods.GET];\n\n if (gets) {\n gets.forEach(() => {\n app.get(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.GET, path);\n });\n }\n }\n\n registerPOST(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const posts = pathItem[methods.POST];\n if (posts) {\n posts.forEach(() => {\n app.post(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.POST, path);\n });\n }\n }\n\n registerPUT(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const puts = pathItem[methods.PUT];\n if (puts) {\n puts.forEach(() => {\n app.put(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.PUT, path);\n });\n }\n }\n\n registerDELETE(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const deletes = pathItem[methods.DELETE];\n if (deletes) {\n deletes.forEach(() => {\n app.delete(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.DELETE, path);\n });\n }\n }\n\n registerPATCH(app, path, map, isLogEnabled) {\n const pathItem = this.parser.getMap()[path];\n const patches = pathItem[methods.PATCH];\n if (patches) {\n patches.forEach(() => {\n app.patch(path, (req, res, next) => {\n requestHandler(this.parser, req, res, next);\n });\n this.logRegisteredPath(isLogEnabled, methods.PATCH, path);\n });\n }\n }\n\n /**\n * Method to log registered path\n */\n logRegisteredPath(isLogEnabled, method, path) {\n if (isLogEnabled) {\n console.log(`REG ${method} ${path}`.magenta);\n }\n }\n}\n"],"file":"sources-router.js"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/core/utils.js"],"names":["isFunction","functionToCheck","getType","toString","call"],"mappings":";;;;;;;AAAO,MAAMA,UAAU,GAAGC,eAAe,IAAI;AAC3C,QAAMC,OAAO,GAAG,EAAhB;AACA,SACED,eAAe,IACfC,OAAO,CAACC,QAAR,CAAiBC,IAAjB,CAAsBH,eAAtB,MAA2C,mBAF7C;AAID,CANM","sourcesContent":["export const isFunction = functionToCheck => {\n const getType = {};\n return (\n functionToCheck &&\n getType.toString.call(functionToCheck) === '[object Function]'\n );\n};\n"],"file":"utils.js"}
1
+ {"version":3,"sources":["../../lib/core/utils.js"],"names":["isFunction","functionToCheck","getType","toString","call"],"mappings":";;;;;;;AAAO,MAAMA,UAAU,GAAIC,eAAD,IAAqB;AAC7C,QAAMC,OAAO,GAAG,EAAhB;AACA,SACED,eAAe,IACfC,OAAO,CAACC,QAAR,CAAiBC,IAAjB,CAAsBH,eAAtB,MAA2C,mBAF7C;AAID,CANM","sourcesContent":["export const isFunction = (functionToCheck) => {\n const getType = {};\n return (\n functionToCheck &&\n getType.toString.call(functionToCheck) === '[object Function]'\n );\n};\n"],"file":"utils.js"}
package/dist/index.js CHANGED
@@ -12,12 +12,15 @@ var _config = _interopRequireDefault(require("./config"));
12
12
 
13
13
  var _app = require("./app");
14
14
 
15
+ var _ws = _interopRequireDefault(require("ws"));
16
+
15
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
18
 
17
19
  const {
18
20
  port
19
21
  } = _config.default;
20
- let server = null;
22
+ let server,
23
+ wsServer = null;
21
24
  /**
22
25
  * Method to start server.
23
26
  *
@@ -25,6 +28,7 @@ let server = null;
25
28
  * @param {Object} serverConfig
26
29
  * @param serverConfig.controlApiUrl ['/api/v1']
27
30
  * @param serverConfig.port [8080]
31
+ * @param serverConfig.webSocketEnabled false
28
32
  */
29
33
 
30
34
  function serverStart(sources, serverConfig = {}) {
@@ -35,6 +39,12 @@ function serverStart(sources, serverConfig = {}) {
35
39
  console.log(`*************************`.yellow);
36
40
  console.log('Server START'.bgGreen.white);
37
41
  });
42
+
43
+ if (serverConfig.webSocketEnabled) {
44
+ wsServer = startWebSocket(server);
45
+ app.initWebSocketApi(wsServer);
46
+ }
47
+
38
48
  return server;
39
49
  }
40
50
  /**
@@ -46,6 +56,30 @@ function serverStop() {
46
56
  if (server) {
47
57
  server.close();
48
58
  }
59
+
60
+ if (wsServer) {
61
+ wsServer.close();
62
+ }
63
+ }
64
+
65
+ function startWebSocket(server) {
66
+ const wsServer = new _ws.default.Server({
67
+ noServer: true
68
+ });
69
+ wsServer.on('connection', socket => {
70
+ console.log('WebSocket client connected');
71
+ socket.on('message', message => {
72
+ console.log('WebSocket client message received');
73
+ console.log(message);
74
+ });
75
+ socket.on('close', () => console.log('WebSocket client disconnected'));
76
+ });
77
+ server.on('upgrade', (request, socket, head) => {
78
+ wsServer.handleUpgrade(request, socket, head, socket => {
79
+ wsServer.emit('connection', socket, request);
80
+ });
81
+ });
82
+ return wsServer;
49
83
  }
50
84
 
51
85
  function getServerPort(serverConfig) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../lib/index.js"],"names":["port","config","server","serverStart","sources","serverConfig","app","App","getServerPort","start","console","log","yellow","bgGreen","white","serverStop","close"],"mappings":";;;;;;;;AAAA;;AACA;;AACA;;;;AAEA,MAAM;AAAEA,EAAAA;AAAF,IAAWC,eAAjB;AACA,IAAIC,MAAM,GAAG,IAAb;AAEA;;;;;;;;;AAQO,SAASC,WAAT,CAAqBC,OAArB,EAA8BC,YAAY,GAAG,EAA7C,EAAiD;AACtD,QAAMC,GAAG,GAAG,IAAIC,QAAJ,CAAQH,OAAR,EAAiBC,YAAjB,CAAZ;AACA,QAAML,IAAI,GAAGQ,aAAa,CAACH,YAAD,CAA1B;AACAH,EAAAA,MAAM,GAAGI,GAAG,CAACG,KAAJ,CAAUT,IAAV,EAAgB,MAAM;AAC7BU,IAAAA,OAAO,CAACC,GAAR,CAAa,oBAAmBX,IAAK,EAAzB,CAA2BY,MAAvC;AACAF,IAAAA,OAAO,CAACC,GAAR,CAAa,2BAAD,CAA4BC,MAAxC;AACAF,IAAAA,OAAO,CAACC,GAAR,CAAY,eAAeE,OAAf,CAAuBC,KAAnC;AACD,GAJQ,CAAT;AAKA,SAAOZ,MAAP;AACD;AAED;;;;;AAGO,SAASa,UAAT,GAAsB;AAC3B,MAAIb,MAAJ,EAAY;AACVA,IAAAA,MAAM,CAACc,KAAP;AACD;AACF;;AAED,SAASR,aAAT,CAAuBH,YAAvB,EAAqC;AACnC,SAAOA,YAAY,CAACL,IAAb,GAAoBK,YAAY,CAACL,IAAjC,GAAwCA,IAA/C;AACD","sourcesContent":["import 'colors';\nimport config from './config';\nimport { App } from './app';\n\nconst { port } = config;\nlet server = null;\n\n/**\n * Method to start server.\n *\n * @param sources\n * @param {Object} serverConfig\n * @param serverConfig.controlApiUrl ['/api/v1']\n * @param serverConfig.port [8080]\n */\nexport function serverStart(sources, serverConfig = {}) {\n const app = new App(sources, serverConfig);\n const port = getServerPort(serverConfig);\n server = app.start(port, () => {\n console.log(`* Server port ${port}`.yellow);\n console.log(`*************************`.yellow);\n console.log('Server START'.bgGreen.white);\n });\n return server;\n}\n\n/**\n * Method to stop server.\n */\nexport function serverStop() {\n if (server) {\n server.close();\n }\n}\n\nfunction getServerPort(serverConfig) {\n return serverConfig.port ? serverConfig.port : port;\n}\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../lib/index.js"],"names":["port","config","server","wsServer","serverStart","sources","serverConfig","app","App","getServerPort","start","console","log","yellow","bgGreen","white","webSocketEnabled","startWebSocket","initWebSocketApi","serverStop","close","ws","Server","noServer","on","socket","message","request","head","handleUpgrade","emit"],"mappings":";;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAEA,MAAM;AAAEA,EAAAA;AAAF,IAAW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EAAP,CAAU,SAAV,EAAsBE,OAAD,IAAa;AAChCf,MAAAA,OAAO,CAACC,GAAR,CAAY,mCAAZ;AACAD,MAAAA,OAAO,CAACC,GAAR,CAAYc,OAAZ;AACD,KAHD;AAIAD,IAAAA,MAAM,CAACD,EAAP,CAAU,OAAV,EAAmB,MAAMb,OAAO,CAACC,GAAR,CAAY,+BAAZ,CAAzB;AACD,GAPD;AASAV,EAAAA,MAAM,CAACsB,EAAP,CAAU,SAAV,EAAqB,CAACG,OAAD,EAAUF,MAAV,EAAkBG,IAAlB,KAA2B;AAC9CzB,IAAAA,QAAQ,CAAC0B,aAAT,CAAuBF,OAAvB,EAAgCF,MAAhC,EAAwCG,IAAxC,EAA+CH,MAAD,IAAY;AACxDtB,MAAAA,QAAQ,CAAC2B,IAAT,CAAc,YAAd,EAA4BL,MAA5B,EAAoCE,OAApC;AACD,KAFD;AAGD,GAJD;AAMA,SAAOxB,QAAP;AACD;;AAED,SAASM,aAAT,CAAuBH,YAAvB,EAAqC;AACnC,SAAOA,YAAY,CAACN,IAAb,GAAoBM,YAAY,CAACN,IAAjC,GAAwCA,IAA/C;AACD","sourcesContent":["import 'colors';\nimport config from './config';\nimport { App } from './app';\nimport ws from 'ws';\n\nconst { port } = config;\nlet server,\n wsServer = null;\n\n/**\n * Method to start server.\n *\n * @param sources\n * @param {Object} serverConfig\n * @param serverConfig.controlApiUrl ['/api/v1']\n * @param serverConfig.port [8080]\n * @param serverConfig.webSocketEnabled false\n */\nexport function serverStart(sources, serverConfig = {}) {\n const app = new App(sources, serverConfig);\n const port = getServerPort(serverConfig);\n server = app.start(port, () => {\n console.log(`* Server port ${port}`.yellow);\n console.log(`*************************`.yellow);\n console.log('Server START'.bgGreen.white);\n });\n\n if (serverConfig.webSocketEnabled) {\n wsServer = startWebSocket(server);\n app.initWebSocketApi(wsServer);\n }\n\n return server;\n}\n\n/**\n * Method to stop server.\n */\nexport function serverStop() {\n if (server) {\n server.close();\n }\n if (wsServer) {\n wsServer.close();\n }\n}\n\nfunction startWebSocket(server) {\n const wsServer = new ws.Server({ noServer: true });\n wsServer.on('connection', (socket) => {\n console.log('WebSocket client connected');\n socket.on('message', (message) => {\n console.log('WebSocket client message received');\n console.log(message);\n });\n socket.on('close', () => console.log('WebSocket client disconnected'));\n });\n\n server.on('upgrade', (request, socket, head) => {\n wsServer.handleUpgrade(request, socket, head, (socket) => {\n wsServer.emit('connection', socket, request);\n });\n });\n\n return wsServer;\n}\n\nfunction getServerPort(serverConfig) {\n return serverConfig.port ? serverConfig.port : port;\n}\n"],"file":"index.js"}
@@ -1,24 +1,24 @@
1
1
  import express from 'express';
2
- import { ACCEPTED } from 'http-status-codes';
2
+ import { StatusCodes } from 'http-status-codes';
3
3
  import config from '../config';
4
4
 
5
5
  const router = express.Router({});
6
6
  const { isLogEnabled } = config;
7
7
 
8
- export const controlApi = sourcesParser => {
8
+ export const controlApi = (sourcesParser) => {
9
9
  router.post('/set', (req, res) => {
10
10
  if (isLogEnabled) {
11
11
  console.log('API set new values'.green);
12
12
  }
13
13
 
14
- req.body.forEach(record => {
14
+ req.body.forEach((record) => {
15
15
  // console.log('new record is >');
16
16
  // console.log(JSON.stringify(record, null, " "));
17
17
  // console.log('< new record is');
18
18
  sourcesParser.switchRecord(record);
19
19
  });
20
20
 
21
- res.status(ACCEPTED).end();
21
+ res.status(StatusCodes.ACCEPTED).end();
22
22
  });
23
23
 
24
24
  router.post('/reset', (req, res) => {
@@ -28,7 +28,7 @@ export const controlApi = sourcesParser => {
28
28
 
29
29
  sourcesParser.resetMap();
30
30
 
31
- res.status(ACCEPTED).end();
31
+ res.status(StatusCodes.ACCEPTED).end();
32
32
  });
33
33
 
34
34
  return router;
@@ -1,4 +1,4 @@
1
- import uuidv1 from 'uuid/v1';
1
+ import { v1 as uuidv1 } from 'uuid';
2
2
 
3
3
  const recordingStore = {};
4
4
  let isRecordingActive = false;
@@ -14,21 +14,21 @@ export const startNewRecording = () => {
14
14
  return uuid;
15
15
  };
16
16
 
17
- export const stopRecording = uuid => {
17
+ export const stopRecording = (uuid) => {
18
18
  const recording = recordingStore[uuid];
19
19
  delete recordingStore[uuid];
20
20
  isRecordingActive = Object.keys(recordingStore).length > 0;
21
21
  return recording;
22
22
  };
23
23
 
24
- export const apiRecorder = apiUrl => {
24
+ export const apiRecorder = (apiUrl) => {
25
25
  return (req, res, next) => {
26
26
  if (isRecordingActive && isNotRecordingApiPath(apiUrl, req.path)) {
27
27
  const originalSend = res.send;
28
- res.send = function(body) {
28
+ res.send = function (body) {
29
29
  originalSend.call(this, body);
30
30
 
31
- Object.keys(recordingStore).forEach(recordUuid => {
31
+ Object.keys(recordingStore).forEach((recordUuid) => {
32
32
  recordingStore[recordUuid].push({
33
33
  url: req.baseUrl,
34
34
  path: req.path,
@@ -36,7 +36,7 @@ export const apiRecorder = apiUrl => {
36
36
  params: req.params,
37
37
  query: req.query,
38
38
  body: req.body,
39
- responseBody: !body ? {} : JSON.parse(body)
39
+ responseBody: !body ? {} : JSON.parse(body),
40
40
  });
41
41
  });
42
42
  };
@@ -7,7 +7,7 @@ export const recordingApi = () => {
7
7
  router.post('/recording/start', (req, res) => {
8
8
  const uuid = startNewRecording();
9
9
  res.send({
10
- id: uuid
10
+ id: uuid,
11
11
  });
12
12
  });
13
13
 
@@ -0,0 +1,27 @@
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 CHANGED
@@ -7,6 +7,7 @@ import { SourcesParser } from './core/sources-parser';
7
7
  import { SourcesRouter } from './core/sources-router';
8
8
  import { recordingApi } from './api/recording';
9
9
  import { apiRecorder } from './api/recording/api-recorder';
10
+ import { websocketApi } from './api/websocket';
10
11
 
11
12
  const { isLogEnabled } = config;
12
13
 
@@ -20,6 +21,7 @@ export class App {
20
21
 
21
22
  this.initMiddleware();
22
23
  this.initControlApi();
24
+ this.initRecordingApi();
23
25
  this.initMocks();
24
26
  }
25
27
 
@@ -50,9 +52,16 @@ export class App {
50
52
 
51
53
  initControlApi() {
52
54
  this.app.use(this.apiUrl, controlApi(this.parser));
55
+ }
56
+
57
+ initRecordingApi() {
53
58
  this.app.use(this.apiUrl, recordingApi());
54
59
  }
55
60
 
61
+ initWebSocketApi(wsServer) {
62
+ this.app.use(this.apiUrl, websocketApi(wsServer));
63
+ }
64
+
56
65
  getApiUrl({ controlApiUrl } = {}) {
57
66
  return controlApiUrl ? controlApiUrl : '/api/v1';
58
67
  }
package/lib/config.js CHANGED
@@ -23,7 +23,7 @@ nconf.defaults({
23
23
  isProduction: process.env.NODE_ENV === 'production',
24
24
  isLogEnabled: process.env.LOGGING === 'ON',
25
25
  locales: ['cs', 'en'],
26
- port: process.env.PORT || 8080
26
+ port: process.env.PORT || 8080,
27
27
  });
28
28
 
29
29
  export default nconf.get();
package/lib/constants.js CHANGED
@@ -3,7 +3,7 @@ export const methods = {
3
3
  PUT: 'PUT',
4
4
  GET: 'GET',
5
5
  DELETE: 'DELETE',
6
- PATCH: 'PATCH'
6
+ PATCH: 'PATCH',
7
7
  };
8
8
 
9
9
  export const REG_ALL = '.*';
@@ -2,7 +2,7 @@ export const MATCH_TYPE = {
2
2
  STRICT: 'strict',
3
3
  SUBSET: 'subset', // some QS match
4
4
  DEFAULT: 'default', // request have QS but bundle doesnt
5
- NO: 'no'
5
+ NO: 'no',
6
6
  };
7
7
 
8
8
  /**
@@ -52,7 +52,7 @@ export const matchRecordPost = (req, bundle) => {
52
52
  // let recordBodyKeysLength = Object.keys(recordBodyValue).length;
53
53
  // if(requestBodyKeysLength === recordBodyKeysLength){
54
54
  let isMatched = true;
55
- Object.keys(recordBodyValue).forEach(key => {
55
+ Object.keys(recordBodyValue).forEach((key) => {
56
56
  let value = recordBodyValue[key];
57
57
  if (
58
58
  JSON.stringify(value) !== JSON.stringify(requestBody[key])
@@ -4,7 +4,7 @@ import MatchingResult, {
4
4
  MATCH_DEFAULT,
5
5
  MATCH_NO,
6
6
  MATCH_STRICT,
7
- MATCH_TYPE
7
+ MATCH_TYPE,
8
8
  } from './MatchingResult';
9
9
 
10
10
  const TYPE_SPECIFIC_REGEX = 'SPECIFIC_REGEX';
@@ -19,7 +19,7 @@ const isEqualQueryParamValues = (masterValues, slaveValues) => {
19
19
  }
20
20
 
21
21
  return [...masterValues].sort().join('') === [...slaveValues].sort().join('');
22
- }
22
+ };
23
23
 
24
24
  /**
25
25
  * exist dont exist dont
@@ -53,7 +53,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
53
53
  if (type === TYPE_SPECIFIC_REGEX) {
54
54
  isSpecific = true;
55
55
  if (!isContained) {
56
- values.forEach(value => {
56
+ values.forEach((value) => {
57
57
  let regex = new RegExp(value);
58
58
 
59
59
  // console.log(name);
@@ -96,7 +96,7 @@ export const getMatchingResultByQueryStrings = (masterQS, slaveQS) => {
96
96
 
97
97
  export const getMatchesByQueryString = (req, definitions) => {
98
98
  let matches = [];
99
- definitions.forEach(record => {
99
+ definitions.forEach((record) => {
100
100
  // console.log(req.query);
101
101
  // console.log(record.queryStringParameters);
102
102
  let result = getMatchingResultByQueryStrings(
@@ -1,12 +1,55 @@
1
- import { NOT_FOUND } from 'http-status-codes';
2
- import { methods } from './../constants';
1
+ import { StatusCodes } from 'http-status-codes';
2
+ import { methods } from '../constants';
3
3
  import { isFunction } from './utils';
4
4
  import { matchRecordPost } from './matchers/http-post-matcher';
5
5
  import { matchRecordGet } from './matchers/http-get-matcher';
6
6
 
7
7
  const APPLICATION_JSON = 'application/json';
8
8
 
9
- export const requestHandler = (sourcesParser, req, res) => {
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) => {
10
53
  let method = req.method;
11
54
  let path = req.route.path;
12
55
  let definitions = sourcesParser.getMap()[path][method];
@@ -51,7 +94,7 @@ export const requestHandler = (sourcesParser, req, res) => {
51
94
  console.log('bundle'.yellow);
52
95
  // myLog(bundle);
53
96
  res
54
- .status(NOT_FOUND)
97
+ .status(StatusCodes.NOT_FOUND)
55
98
  .send('NOT FOUND - no reaponse but why? Look to console.')
56
99
  .end();
57
100
  return;
@@ -63,18 +106,18 @@ export const requestHandler = (sourcesParser, req, res) => {
63
106
  }
64
107
 
65
108
  if (!!response.headers) {
66
- response.headers.forEach(headerItem => {
109
+ response.headers.forEach((headerItem) => {
67
110
  res.setHeader(headerItem['name'], headerItem['values'][0]);
68
111
  });
69
112
  }
70
113
 
71
- res.status(response.statusCode).type(APPLICATION_JSON);
114
+ res.status(response.statusCode).type(response.contentType || APPLICATION_JSON);
72
115
 
73
116
  if (response.delay) {
74
117
  setTimeout(() => {
75
- res.send(response.body);
118
+ sendResponse(response, res, next);
76
119
  }, response.delay);
77
120
  } else {
78
- res.send(response.body);
121
+ sendResponse(response, res, next);
79
122
  }
80
123
  };
@@ -47,7 +47,7 @@ export class SourcesParser {
47
47
  }
48
48
 
49
49
  this.map = {};
50
- sources.forEach(source => {
50
+ sources.forEach((source) => {
51
51
  this.setSource(this.map, source);
52
52
  });
53
53
  this.sortMap();
@@ -61,7 +61,7 @@ export class SourcesParser {
61
61
  *
62
62
  */
63
63
  setSource(map, source) {
64
- source.forEach(record => {
64
+ source.forEach((record) => {
65
65
  this.setRecord(map, record);
66
66
  });
67
67
  }
@@ -115,7 +115,7 @@ export class SourcesParser {
115
115
  * Method to sort map
116
116
  */
117
117
  sortMap() {
118
- Object.keys(this.map).forEach(path => {
118
+ Object.keys(this.map).forEach((path) => {
119
119
  let pathItem = this.map[path];
120
120
  let gets = pathItem[methods.GET];
121
121
  if (gets) {
@@ -152,21 +152,21 @@ function isQueryStringEqual(qs1, qs2) {
152
152
  let qs1ref = {};
153
153
  let qs2ref = {};
154
154
 
155
- qs1.forEach(item => {
155
+ qs1.forEach((item) => {
156
156
  qs1ref[item['name']] = item['values'];
157
157
  });
158
158
 
159
- qs2.forEach(item => {
159
+ qs2.forEach((item) => {
160
160
  qs2ref[item['name']] = item['values'];
161
161
  });
162
162
 
163
- Object.keys(qs1ref).forEach(name => {
163
+ Object.keys(qs1ref).forEach((name) => {
164
164
  let values = qs1ref[name];
165
165
  let qs2Values = qs2ref[name];
166
166
  if (qs2Values.length != values.length) {
167
167
  isEqual = false;
168
168
  } else {
169
- values.forEach(value => {
169
+ values.forEach((value) => {
170
170
  if (qs2Values.indexOf(value) === -1) {
171
171
  isEqual = false;
172
172
  }
@@ -192,7 +192,7 @@ function compare(a, b) {
192
192
  a.queryStringParameters.forEach((qs, index) => {
193
193
  if (qs['values'].indexOf(REG_ALL) !== -1) aSpecificCount++;
194
194
  });
195
- b.queryStringParameters.forEach(qs => {
195
+ b.queryStringParameters.forEach((qs) => {
196
196
  if (qs['values'].indexOf(REG_ALL) !== -1) bSpecificCount++;
197
197
  });
198
198
  return aSpecificCount - bSpecificCount;
@@ -9,16 +9,16 @@ describe('source-parser', () => {
9
9
  {
10
10
  request: {
11
11
  method: 'GET',
12
- path: '/api/test'
12
+ path: '/api/test',
13
13
  },
14
14
  response: {
15
15
  statusCode: 200,
16
16
  body: JSON.stringify({
17
- key: 'value'
18
- })
19
- }
20
- }
21
- ]
17
+ key: 'value',
18
+ }),
19
+ },
20
+ },
21
+ ],
22
22
  ];
23
23
 
24
24
  // when
@@ -11,7 +11,7 @@ export class SourcesRouter {
11
11
 
12
12
  registerSources(app, isLogEnabled) {
13
13
  const map = this.parser.getMap();
14
- Object.keys(map).forEach(path => {
14
+ Object.keys(map).forEach((path) => {
15
15
  this.registerGET(app, path, isLogEnabled);
16
16
  this.registerPOST(app, path, isLogEnabled);
17
17
  this.registerPUT(app, path, isLogEnabled);
@@ -26,8 +26,8 @@ export class SourcesRouter {
26
26
 
27
27
  if (gets) {
28
28
  gets.forEach(() => {
29
- app.get(path, (req, res) => {
30
- requestHandler(this.parser, req, res);
29
+ app.get(path, (req, res, next) => {
30
+ requestHandler(this.parser, req, res, next);
31
31
  });
32
32
  this.logRegisteredPath(isLogEnabled, methods.GET, path);
33
33
  });
@@ -39,8 +39,8 @@ export class SourcesRouter {
39
39
  const posts = pathItem[methods.POST];
40
40
  if (posts) {
41
41
  posts.forEach(() => {
42
- app.post(path, (req, res) => {
43
- requestHandler(this.parser, req, res);
42
+ app.post(path, (req, res, next) => {
43
+ requestHandler(this.parser, req, res, next);
44
44
  });
45
45
  this.logRegisteredPath(isLogEnabled, methods.POST, path);
46
46
  });
@@ -52,8 +52,8 @@ export class SourcesRouter {
52
52
  const puts = pathItem[methods.PUT];
53
53
  if (puts) {
54
54
  puts.forEach(() => {
55
- app.put(path, (req, res) => {
56
- requestHandler(this.parser, req, res);
55
+ app.put(path, (req, res, next) => {
56
+ requestHandler(this.parser, req, res, next);
57
57
  });
58
58
  this.logRegisteredPath(isLogEnabled, methods.PUT, path);
59
59
  });
@@ -65,8 +65,8 @@ export class SourcesRouter {
65
65
  const deletes = pathItem[methods.DELETE];
66
66
  if (deletes) {
67
67
  deletes.forEach(() => {
68
- app.delete(path, (req, res) => {
69
- requestHandler(this.parser, req, res);
68
+ app.delete(path, (req, res, next) => {
69
+ requestHandler(this.parser, req, res, next);
70
70
  });
71
71
  this.logRegisteredPath(isLogEnabled, methods.DELETE, path);
72
72
  });
@@ -78,8 +78,8 @@ export class SourcesRouter {
78
78
  const patches = pathItem[methods.PATCH];
79
79
  if (patches) {
80
80
  patches.forEach(() => {
81
- app.patch(path, (req, res) => {
82
- requestHandler(this.parser, req, res);
81
+ app.patch(path, (req, res, next) => {
82
+ requestHandler(this.parser, req, res, next);
83
83
  });
84
84
  this.logRegisteredPath(isLogEnabled, methods.PATCH, path);
85
85
  });
@@ -87,7 +87,7 @@ export class SourcesRouter {
87
87
  }
88
88
 
89
89
  /**
90
- * Method to log registred path
90
+ * Method to log registered path
91
91
  */
92
92
  logRegisteredPath(isLogEnabled, method, path) {
93
93
  if (isLogEnabled) {