Server for HTTP fixtures
This was built to make common responses from an API consistent across tests.
For convenience, there are methods which setup/teardown a server for mocha
(see factory.run
).
Getting Started
Install the module with: npm install fixed-server
// Create a server that we can setup/teardown during testsvar assert = ;var request = ;var FixedServer = ;var fixedServer = port: 1337; // Create a fixture that we can refer to during the test// DEV: `method` and `route` are done via `express` which allows for complex methods/routingfixedServer; // Start our test;
Documentation
fixed-server
exposes FixedServerFactory
via its module.exports
. Under the hood, FixedServer
runs via express
.
new FixedServerFactory(options)
Constructor for creating new FixedServer's
- options
Object
- Container for options- port
Number
- Port to run createdFixedServer's
from
- port
FixedServerFactory.fromFile(filepath, options)
Helper to quickly generate a server with fixtures from a file
- filepath
String
- Path to fixtures to load in- This will be loaded via
require
and passed in tofactory.addFixtures
- This will be loaded via
- option
Object
- Options to pass toFixedServerFactory
constructor
factory.addFixture(name, params)
Add a new fixture to the list of potential fixture to load into child servers.
- name
String
- Key to store fixture under - params
Object
- Container for fixture info- method
String
- Lowercase HTTP method to runparams.response
under (e.g.get
,post
,put
)- Any valid
express
method is accepted
- Any valid
- route
String|RegExp
- Route to runparams.response
under (e.g./hello
) - response
Function
-express
middleware that will handle request and generate response- Function signature must be
(req, res)
as is expected inexpress
- Function signature must be
- method
factory.addFixtures(obj)
Add multiple fixtures to our list of fixtures
- obj
Object
- Container for multiple fixtures- Each key-value pair will be used as
name
andparams
respectively forFixedServer.addFixtures
- Each key-value pair will be used as
factory.createServer(fixtureNames)
Create a FixedServer
with fixtureNames
running on it
- fixtureNames
String|String[]
- Single fixture name or array of fixture names to load into server- Each of these will be loaded via
server.installFixture
- Each of these will be loaded via
factory.run(fixtureNames)
Helper method for running server inside of mocha
tests
- fixtureNames
String|String[]
- Information to pass ontofactory.createServer
FixedServer(options)
Create a server to host fixtures on
- options
Object
- Container for options- port
Number
- Port to run server from via.listen()
- port
server.listen(port)
Start listening for requests
- port
Number
- Port to start listening against. If not provided, it will attempt to useoptions.port
.
server.destroy(cb)
Tear down the server
- cb
Function
- Optional error-first callback to run when the server teardown is completed
server.installFixture(fixture)
Add a new route to the server
- fixture
Object
- Container for route parameters- method
String
- Lowercase HTTP method to runparams.response
under (e.g.get
,post
,put
)- Any valid
express
method is accepted
- Any valid
- route
String|RegExp
- Route to runfixture.response
under (e.g./hello
) - response
Function
-express
middleware that will handle request and generate response- Function signature must be
(req, res)
as is expected inexpress
- Function signature must be
- method
server;// converts to;
server.run()
Helper method for running the server inside of mocha
tests
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
License
Copyright (c) 2014 Uber
Licensed under the MIT license.