Comparing version 0.1.4 to 0.1.5
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
.option("-c, --config <path>", "Path to config.json file.", __dirname + "/../config.json") | ||
.option("-O, --show-output", "Enable console logging. Default is true.", true) | ||
.option("-q, --quiet", "Disable console logging") | ||
.option("-p, --port <port>", "Port that the http mock server will use. Default is 8888.", "8888") | ||
@@ -15,4 +15,4 @@ .parse(process.argv); | ||
var options = {}; | ||
commander.port && (options.serverPort = commander.port); | ||
commander.showOutput && (options.output = commander.showOutput); | ||
options.port = commander.port; | ||
options.quiet = !!commander.quiet; | ||
@@ -19,0 +19,0 @@ var apiMocker = ApiMocker.createServer(options) |
{ | ||
"note": "This is a sample config file. You should change the mockDirectory to a more reasonable path.", | ||
"mockDirectory": "/usr/local/lib/node_modules/apimocker/samplemocks/", | ||
"output": true, | ||
"quiet": false, | ||
"port": "7878", | ||
@@ -6,0 +6,0 @@ "latency": 50, |
@@ -5,7 +5,7 @@ var express = require('express'), | ||
fs = require("fs"), | ||
apiMocker = {}, | ||
defaults = { | ||
apiMocker = {}; | ||
apiMocker.defaults = { | ||
"port": "8888", | ||
"mockDirectory": "./mocks/", | ||
"output": false, | ||
"allowedDomains": ["*"], | ||
@@ -19,9 +19,10 @@ "webServices": { | ||
apiMocker.createServer = function(options) { | ||
options = options || {}; | ||
apiMocker.express = express(); | ||
apiMocker.express.use(express.bodyParser()); | ||
apiMocker.express.use(apiMocker.corsMiddleware); | ||
apiMocker.options = {}; | ||
apiMocker.defaults = defaults; | ||
apiMocker.options = _.defaults(options, apiMocker.defaults); | ||
apiMocker.log = function(msg) { | ||
if (apiMocker.options.output + "" === "true") { | ||
if (!apiMocker.options.quiet) { | ||
console.log(msg); | ||
@@ -42,3 +43,2 @@ } | ||
} | ||
_.defaults(apiMocker.options, require(apiMocker.configFilePath), apiMocker.defaults); | ||
return apiMocker; | ||
@@ -50,7 +50,8 @@ }; | ||
apiMocker.log("Loading config file: " + apiMocker.configFilePath); | ||
// Since the configFilePath can be set in different ways, | ||
// I may need to delete from cache in different ways. | ||
delete require.cache[apiMocker.configFilePath]; | ||
delete require.cache[require.resolve(apiMocker.configFilePath)]; | ||
_.extend(apiMocker.options, apiMocker.defaults, require(apiMocker.configFilePath)); | ||
// Switched to use fs.readFileSync instead of "require" | ||
// this makes testing easier, and avoids messing with require cache. | ||
var newOptions = _.clone(apiMocker.defaults), | ||
configJson = JSON.parse(fs.readFileSync(apiMocker.configFilePath)); | ||
newOptions = _.extend(newOptions, apiMocker.options, configJson); | ||
apiMocker.options = newOptions; | ||
apiMocker.setRoutes(apiMocker.options.webServices); | ||
@@ -174,7 +175,6 @@ } else { | ||
apiMocker.start = function (port) { | ||
port = port || apiMocker.options.port; | ||
apiMocker.createAdminServices(); | ||
apiMocker.loadConfigFile(); | ||
port = port || apiMocker.options.port; | ||
apiMocker.express.listen(port); | ||
apiMocker.log("Mock server listening on port " + port); | ||
@@ -181,0 +181,0 @@ return apiMocker; |
{ | ||
"name": "apimocker", | ||
"description": "Simple HTTP server using express, for server API mocking.", | ||
"version": "0.1.4", | ||
"description": "Simple HTTP server that returns mock service API responses to your front end.", | ||
"version": "0.1.5", | ||
"author": { | ||
@@ -15,3 +15,9 @@ "name": "Greg Stroup", | ||
"devDependencies": { | ||
"mocha": ">=1.8.2" | ||
"mocha": ">=1.8.2", | ||
"grunt": "~0.4.0", | ||
"grunt-simple-mocha": ">=0.3.2", | ||
"grunt-contrib-watch": ">=0.3.1", | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"chai": ">=1.5.0", | ||
"sinon": ">=1.6.0" | ||
}, | ||
@@ -21,3 +27,3 @@ "bin": "./bin/apimocker.js", | ||
"keywords": [ | ||
"express", "mock", "stub", | ||
"express", "mock", "stub", "REST", "SOAP", | ||
"testing", "functional", "api" | ||
@@ -24,0 +30,0 @@ ], |
@@ -15,3 +15,3 @@ # apimocker | ||
## Usage | ||
apimocker [-c, --config \<path\>] [-O, --output] [-p \<port\>] | ||
apimocker [-c, --config \<path\>] [-q, --quiet] [-p \<port\>] | ||
@@ -22,5 +22,5 @@ Out of the box, you can just run "apimocker" with no arguments. | ||
Then you can visit "http://localhost:7878/first" in your browser to see it work. | ||
The output and port options can also be set in the config.json file. | ||
The quiet and port options can also be set in the config.json file. | ||
Values from config.json will override values from command line. | ||
After you get up and running, you should put your mock responses in a better location. | ||
After you get up and running, you should put your config.json and mock responses in a better location. | ||
It's not a good idea to keep them under the "node_modules" directory. | ||
@@ -86,3 +86,4 @@ Make sure another process is not already using the port you want. | ||
"serviceUrl":"third", | ||
"mockFile":"queen.xml" | ||
"mockFile":"queen.xml", | ||
"latency": 100 | ||
} | ||
@@ -97,4 +98,7 @@ ``` | ||
## Contributors | ||
Run "grunt" in the root "apimocker" directory to start the grunt watch task. This will run JSHint and mocha tests. | ||
## Acknowledgements | ||
Big thanks to magalhas for his httpd-mock project. This gave me a great starting point. | ||
Also thanks to clafonta and the Mockey project for inspiration. |
143
test/test.js
@@ -1,36 +0,123 @@ | ||
// run "mocha" in this test directory to execute. | ||
// run "grunt test", or run "mocha" in this test directory to execute. | ||
describe('unit tests', function() { | ||
var chai = require("chai"), // better assertions than node offers. | ||
apiMocker = require("../lib/apimocker.js"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
assert = chai.assert, | ||
expect = chai.expect, | ||
sinon = require("sinon"), | ||
testConfig = { | ||
"mockDirectory": "foo/bar/samplemocks/", | ||
"quiet": true, | ||
"port": "7879", | ||
"latency": 50, | ||
"allowedDomains": ["abc"], | ||
"webServices": { | ||
"get": { | ||
"first": "king.json", | ||
"nested/ace": "ace.json", | ||
"var/:id": "xml/queen.xml" | ||
}, | ||
"post": { | ||
"king": "king.json" | ||
}, | ||
"all": { | ||
"queen": "xml/queen.xml" | ||
} | ||
} | ||
}; | ||
chai.Assertion.includeStack = true; | ||
var assert = require("assert"), | ||
apiMocker = require("../lib/apimocker.js"), | ||
path = require("path"); | ||
describe('createServer', function() { | ||
it('sets defaults when no options are passed in', function() { | ||
var mocker = apiMocker.createServer(); | ||
expect(mocker.options.port).to.equal("8888"); | ||
expect(mocker.options.mockDirectory).to.equal("./mocks/"); | ||
expect(mocker.options.allowedDomains.length).to.equal(1); | ||
expect(mocker.options.allowedDomains[0]).to.equal("*"); | ||
expect(mocker.options.webServices.get).to.be.an('object'); | ||
expect(mocker.options.webServices.post).to.be.an('object'); | ||
}); | ||
describe('setConfigFile', function() { | ||
var mocker = apiMocker.createServer(); | ||
it('overrides defaults with command line args', function() { | ||
var mocker = apiMocker.createServer({port:1234, quiet: true, foo: "bar"}); | ||
expect(mocker.options.port).to.equal(1234); | ||
expect(mocker.options.mockDirectory).to.equal("./mocks/"); | ||
expect(mocker.options.allowedDomains[0]).to.equal("*"); | ||
expect(mocker.options.webServices.get).to.be.an('object'); | ||
expect(mocker.options.webServices.post).to.be.an('object'); | ||
expect(mocker.options.quiet).to.equal(true); | ||
expect(mocker.options.foo).to.equal("bar"); | ||
}); | ||
}); | ||
beforeEach(function() { | ||
delete mocker.configFilePath; | ||
}); | ||
describe('setConfigFile', function() { | ||
var mocker = apiMocker.createServer(); | ||
it('should set a relative path correctly using node path resolver', function() { | ||
assert.equal(path.resolve("../config.json"), mocker.setConfigFile("../config.json").configFilePath); | ||
}); | ||
beforeEach(function() { | ||
delete mocker.configFilePath; | ||
}); | ||
it('should set an absolute path correctly', function() { | ||
var absolutePath = path.resolve("../config.json"); | ||
assert.equal(absolutePath, mocker.setConfigFile(absolutePath).configFilePath); | ||
}); | ||
after(function() { | ||
delete mocker.configFilePath; | ||
}); | ||
it('sets no path, if none is passed in', function() { | ||
assert.equal(undefined, mocker.setConfigFile().configFilePath); | ||
}); | ||
}); | ||
it('should set a relative path correctly using node path resolver', function() { | ||
// var mocker = apiMocker.createServer(); | ||
assert.equal(path.resolve("../config.json"), apiMocker.setConfigFile("../config.json").configFilePath); | ||
}); | ||
describe("loadConfigFile", function() { | ||
var mocker = apiMocker.createServer(); | ||
mocker.setConfigFile(path.resolve("../config.json")); | ||
it("TODO: loads from options, defaults, config file, etc...", function() { | ||
mocker.loadConfigFile(); | ||
assert.equal("abc", "abc"); | ||
}); | ||
it('should set an absolute path correctly', function() { | ||
var absolutePath = "/foo/bar/config.json"; | ||
expect(apiMocker.setConfigFile(absolutePath).configFilePath).to.equal(absolutePath); | ||
}); | ||
it('sets no path, if none is passed in', function() { | ||
expect(apiMocker.setConfigFile().configFilePath).to.equal(undefined); | ||
}); | ||
}); | ||
describe("loadConfigFile", function() { | ||
var fsStub; | ||
beforeEach(function createFsStub() { | ||
fsStub = sinon.stub(fs, "readFileSync"); // fsStub is a function | ||
}); | ||
afterEach(function restoreFs() { | ||
fsStub.restore(); | ||
}); | ||
it("sets options from mock in-memory config file", function() { | ||
var mocker = apiMocker.createServer({quiet: true}); | ||
fsStub.returns(JSON.stringify(testConfig)); | ||
mocker.setConfigFile("any value"); | ||
mocker.loadConfigFile(); | ||
expect(mocker.options.port).to.equal(testConfig.port); | ||
expect(mocker.options.mockDirectory).to.equal(testConfig.mockDirectory); | ||
expect(mocker.options.allowedDomains[0]).to.equal(testConfig.allowedDomains[0]); | ||
expect(mocker.options.webServices).to.deep.equal(testConfig.webServices); | ||
expect(mocker.options.quiet).to.equal(true); | ||
expect(mocker.options.latency).to.equal(testConfig.latency); | ||
}); | ||
it("combines values from defaults, options, and config file", function() { | ||
var mocker = apiMocker.createServer({quiet: true, test: "fun", port: 2323}); | ||
fsStub.returns(JSON.stringify({port: 8765, latency: 99})); | ||
mocker = mocker.setConfigFile("another abitrary value"); | ||
mocker.loadConfigFile(); | ||
// value from config file | ||
expect(mocker.options.port).to.equal(8765); | ||
expect(mocker.options.latency).to.equal(99); | ||
// value from defaults | ||
expect(mocker.options.allowedDomains[0]).to.equal("*"); | ||
expect(mocker.options.webServices).to.deep.equal(mocker.defaults.webServices); | ||
// value from options passed in to createServer: | ||
expect(mocker.options.test).to.equal("fun"); | ||
}); | ||
}); | ||
}); |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
21709
13
450
101
2
7
1