A Node.js library with tools to allow for the creation of mock web servers for testing with mocks and real web servers.
The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.
Visit NPMJS.org for a full list of Spur Framework libraries >>
Dependencies:
$ npm install --save coffee-script
$ npm install --save spur-ioc spur-config spur-common spur-web
Module:
$ npm install spur-mockserver --save
This example will only show the files that show unique configurations to a mock server. For a fully detailed example, please view the example here.
path = require "path"
spur = require "spur-ioc"
spurCommon = require "spur-common"
spurWeb = require "spur-web"
spurMockserver = require "spur-mockserver"
spurConfig = require "spur-config"
registerConfig = require "spur-common/registerConfig"
module.exports = ()->
ioc = spur.create("spur-mockserver-example")
registerConfig(ioc, path.join(__dirname, "./config"))
ioc.merge(spurCommon())
ioc.merge(spurWeb())
ioc.merge(spurMockserver())
ioc.registerFolders __dirname, [
"mocks"
"runtime"
]
ioc
module.exports = (MockWebServer)->
new class WebServer extends MockWebServer
By creating files with the ending name of *MockEndPoint.coffee
, it will self register as a mock controller. You can have multiple MockEndpoint files, as long as they don't share the same endpoint urls.
You can also have multiple request handlers in the same MockEndpoint and change which loads dynamically.
module.exports = (MockEndpoint) ->
new class UserMockEndpoint extends MockEndpoint
method: -> MockEndpoint.METHODS.GET
url: -> "/user/:id"
default:(req, res, next) ->
userId = parseInt(req.params.id)
user = {
id: userId
name: "User Name"
statusCode: 200
}
res.status(user.statusCode).json(user)
injector = require("./src/injector")
injector().inject (WebServer) ->
# Starts the web server by loading all the default methods in the MockEndPoints
WebServer.startWithDefaults()
While you can have a standalone mock server, sometimes it's needed to use mock endpoints in an actual application. This scenario could be due to the need to be able to work on parts of the REST API that are defined and mock out parts that are not completely defined.
The following examples show how you mix them by adding a few calls to your web server app based on BaseWebServer defined in Spur-Web. For it's full configuration, take a look at the documentation in Spur-Web, but the following are a highlight of the dependency configuration needed to make it work.
path = require "path"
spur = require "spur-ioc"
spurCommon = require "spur-common"
spurWeb = require "spur-web"
spurMockserver = require "spur-mockserver"
spurConfig = require "spur-config"
registerConfig = require "spur-common/registerConfig"
module.exports = ()->
ioc = spur.create("spur-mockserver-example")
# ... other dependencies
ioc.merge(spurMockserver())
ioc.registerFolders __dirname, [
"mocks"
"runtime"
]
ioc
module.exports = (BaseWebServer, config)->
new class MockWebServer extends BaseWebServer
constructor:->
super
@useDefaults = config.useMockDefaults
registerMiddleware:->
super
@registerMockEndpoints()
registerMockEndpoints:->
Logger.log "Attempting to register mock-endpoints - Use Defaults (#{@useDefaults})"
MockEndpointRegistration.register(@app, @, @useDefaults)
setUseDefaults:(@useDefaults = false)->
startWithDefaults:->
@setUseDefaults(true)
@start()
Please send in pull requests and they will be reviewed in a timely manner. Please review this generic guide to submitting a good pull requests. The only things we ask in addition are the following:
- Please submit small pull requests
- Provide a good description of the changes
- Code changes must include tests
- Be nice to each other in comments. 😇
The majority of the settings are controlled using an EditorConfig configuration file. To use it please download a plugin for your editor of choice.
To run the test suite, first install the dependancies, then run npm test
$ npm install
$ npm test