@kaluza/mock-server
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -55,4 +55,10 @@ "use strict"; | ||
}; | ||
var Method; | ||
(function (Method) { | ||
Method["GET"] = "GET"; | ||
Method["POST"] = "POST"; | ||
})(Method || (Method = {})); | ||
var MockServer = /** @class */ (function () { | ||
function MockServer(port, config) { | ||
this.endpoints = new Map(); | ||
this.port = port; | ||
@@ -63,22 +69,6 @@ this.config = __assign({}, defaultConfig, config); | ||
MockServer.prototype.get = function (path, response) { | ||
var endpointRecord = { | ||
calls: [] | ||
}; | ||
var handler = typeof response === 'function' | ||
? response | ||
: function (req, res) { return res.json(response); }; | ||
this.app.get(path, middleware_1.recordCalls(endpointRecord.calls), handler); | ||
this.log("Handler added for path GET " + path); | ||
return endpointRecord; | ||
return this.createEndpoint(Method.GET, path, response); | ||
}; | ||
MockServer.prototype.post = function (path, response) { | ||
var endpointRecord = { | ||
calls: [] | ||
}; | ||
var handler = typeof response === 'function' | ||
? response | ||
: function (req, res) { return res.json(response); }; | ||
this.app.post(path, middleware_1.recordCalls(endpointRecord.calls), handler); | ||
this.log("Handler added for path POST " + path); | ||
return endpointRecord; | ||
return this.createEndpoint(Method.POST, path, response); | ||
}; | ||
@@ -112,5 +102,9 @@ MockServer.prototype.start = function () { | ||
MockServer.prototype.reset = function () { | ||
if (this.server.address()) | ||
throw new Error('mockserver: stop the server before resetting'); | ||
this.createApp(); | ||
var _this = this; | ||
this.log('Resetting MockServer'); | ||
this.endpoints = new Map(); | ||
this.get('/mockserver', function (req, res) { | ||
res.send("MockServer is running on port " + _this.port); | ||
}); | ||
this.recreateRouter(); | ||
}; | ||
@@ -120,4 +114,4 @@ MockServer.prototype.createApp = function () { | ||
this.app = express(); | ||
this.app.use(middleware_1.logger(this.log.bind(this))); | ||
this.app.use(bodyParser.json()); | ||
this.router = express.Router(); | ||
this.app.use(function (req, res, next) { return _this.router(req, res, next); }); | ||
this.get('/mockserver', function (req, res) { | ||
@@ -127,2 +121,28 @@ res.send("MockServer is running on port " + _this.port); | ||
}; | ||
MockServer.prototype.createEndpoint = function (method, path, response) { | ||
var record = { calls: [] }; | ||
var handler = typeof response === 'function' | ||
? response | ||
: function (req, res) { return res.json(response); }; | ||
this.endpoints.set(path, { method: method, handler: handler, record: record }); | ||
this.recreateRouter(); | ||
this.log("Handler added for path " + method + " " + path); | ||
return record; | ||
}; | ||
MockServer.prototype.recreateRouter = function () { | ||
var _this = this; | ||
this.router = express.Router(); | ||
this.router.use(middleware_1.logger(this.log.bind(this))); | ||
this.router.use(bodyParser.json()); | ||
this.endpoints.forEach(function (endpoint, path) { | ||
switch (endpoint.method) { | ||
case Method.GET: | ||
_this.router.get(path, middleware_1.recordCalls(endpoint.record.calls), endpoint.handler); | ||
break; | ||
case Method.POST: | ||
_this.router.post(path, middleware_1.recordCalls(endpoint.record.calls), endpoint.handler); | ||
break; | ||
} | ||
}); | ||
}; | ||
MockServer.prototype.log = function (message) { | ||
@@ -129,0 +149,0 @@ this.config.enableLogging && console.log(message); |
{ | ||
"name": "@kaluza/mock-server", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "dist/mockserver.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -49,2 +49,4 @@ # Node Mock Server | ||
If two endpoints are created on the same path, the most recently created one will take precedence. | ||
#### server.post(path, response) | ||
@@ -56,2 +58,4 @@ | ||
If two endpoints are created on the same path, the most recently created one will take precedence. | ||
#### server.reset() | ||
@@ -70,2 +74,3 @@ | ||
* `query` - the query params in the request | ||
* `headers` - the request headers | ||
* `body` - the request body, for calls that have one |
11580
167
74