Comparing version 0.1.3 to 0.1.4
var express = require('express'), | ||
_ = require("underscore"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
apiMocker = {}, | ||
@@ -9,2 +10,3 @@ defaults = { | ||
"output": false, | ||
"allowedDomains": ["*"], | ||
"webServices": { | ||
@@ -19,2 +21,3 @@ "get": {}, | ||
apiMocker.express.use(express.bodyParser()); | ||
apiMocker.express.use(apiMocker.corsMiddleware); | ||
apiMocker.options = {}; | ||
@@ -52,3 +55,2 @@ apiMocker.defaults = defaults; | ||
apiMocker.setRoutes(apiMocker.options.webServices); | ||
apiMocker.log("latency: " + apiMocker.options.latency); | ||
} else { | ||
@@ -61,2 +63,8 @@ apiMocker.log("No config file path set."); | ||
apiMocker.express.all("/admin/reload", function(req, res) { | ||
// need to wipe out all the old routes, or express will get confused. | ||
var verbs = _.keys(apiMocker.express.routes); | ||
_.each(verbs, function(verb) { | ||
apiMocker.express.routes[verb] = []; | ||
}); | ||
apiMocker.createAdminServices(); | ||
apiMocker.loadConfigFile(); | ||
@@ -77,3 +85,5 @@ res.writeHead(200, {"Content-Type": "application/json"}); | ||
newRoute.mockFile = req.param('mockFile'); | ||
newRoute.latency = req.param('latency'); | ||
} | ||
apiMocker.removeRoute(newRoute); | ||
apiMocker.setRoute(newRoute); | ||
@@ -84,2 +94,3 @@ // also need to save in our webServices object. | ||
} | ||
delete apiMocker.options.webServices[newRoute.verb][newRoute.serviceUrl]; | ||
apiMocker.options.webServices[newRoute.verb][newRoute.serviceUrl] = newRoute.mockFile; | ||
@@ -100,3 +111,4 @@ | ||
"mockFile": webServices[verb][key], | ||
"verb": verb | ||
"verb": verb, | ||
"latency": apiMocker.options.latency ? apiMocker.options.latency : 0 | ||
}); | ||
@@ -116,9 +128,18 @@ }); | ||
apiMocker.setRoute = function(options) { | ||
apiMocker.log("Setting route: " + options.verb.toUpperCase() + " " + options.serviceUrl + " : " + options.mockFile); | ||
var latency = options.latency ? options.latency : 0; | ||
apiMocker.express[options.verb]("/" + options.serviceUrl, function(req, res) { | ||
if (apiMocker.options.latency && apiMocker.options.latency > 0) { | ||
// apiMocker.log("Latency set to: " + apiMocker.options.latency); | ||
if (options.latency && options.latency > 0) { | ||
setTimeout(function() { | ||
res.sendfile(apiMocker.getMockPath(options), {root: apiMocker.options.mockDirectory}); | ||
}, apiMocker.options.latency); | ||
// res.type("text/xml"); | ||
// apiMocker.log("sending text/xml"); | ||
// var mockPath = path.join(apiMocker.options.mockDirectory, apiMocker.getMockPath(options)); | ||
// fs.readFile(mockPath, {encoding: "utf8"}, function(err, data) { | ||
// if (err) throw err; | ||
// // apiMocker.log("xml data: " + data); | ||
// var buff = new Buffer(data, 'utf8'); | ||
// res.send(buff); | ||
// }); | ||
}, options.latency); | ||
} else { | ||
@@ -128,4 +149,30 @@ res.sendfile(apiMocker.getMockPath(options), {root: apiMocker.options.mockDirectory}); | ||
}); | ||
apiMocker.log("Set route: " + options.verb.toUpperCase() + " " + options.serviceUrl + " : " + | ||
options.mockFile + " " + options.latency + " ms"); | ||
}; | ||
apiMocker.removeRoute = function(options) { | ||
var verbs = [options.verb]; | ||
if (options.verb === "all") { | ||
verbs = _.keys(apiMocker.express.routes); | ||
} | ||
_.each(verbs, function(verb) { | ||
var routesForVerb = apiMocker.express.routes[verb]; | ||
routesForVerb = _.reject(routesForVerb, function(path) { | ||
return path.path === "/" + options.serviceUrl; | ||
}); | ||
apiMocker.express.routes[verb] = routesForVerb; | ||
}); | ||
// apiMocker.log(apiMocker.express.routes); | ||
}; | ||
// CORS middleware | ||
apiMocker.corsMiddleware = function(req, res, next) { | ||
res.header('Access-Control-Allow-Origin', apiMocker.options.allowedDomains); | ||
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); | ||
res.header('Access-Control-Allow-Headers', 'Content-Type'); | ||
next(); | ||
}; | ||
apiMocker.start = function (port) { | ||
@@ -136,3 +183,3 @@ port = port || apiMocker.options.port; | ||
apiMocker.express.listen(port); | ||
apiMocker.log("Mock server listening on port " + port); | ||
@@ -139,0 +186,0 @@ return apiMocker; |
{ | ||
"name": "apimocker", | ||
"description": "Simple HTTP server using express, for server API mocking.", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": { | ||
@@ -14,2 +14,5 @@ "name": "Greg Stroup", | ||
}, | ||
"devDependencies": { | ||
"mocha": ">=1.8.2" | ||
}, | ||
"bin": "./bin/apimocker.js", | ||
@@ -16,0 +19,0 @@ "files": "./samplemocks", |
@@ -42,3 +42,3 @@ # apimocker | ||
See the sample config.json file in this package. Latency (ms) can be set to simulate slow service responses. | ||
mockDirectory value should be an absolute path. | ||
mockDirectory value should be an absolute path. Allowed domains can be set to restrict CORS requests to certain domains. | ||
```js | ||
@@ -50,2 +50,3 @@ { | ||
"port": "7878", | ||
"allowedDomains": ["*"], | ||
"latency": 500, | ||
@@ -52,0 +53,0 @@ "webServices": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13473
10
231
97
1
4