On-The-Fly Mock JSON Server
$ npm install -g caddis
$ caddis -h
usage: caddis [action] [port]
Starts a server at http://localhost:<port> as a daemon, exposing an api to post JSON to in order to mock a RESTful service.
Defaults to port 3001 unless --port option provided.
actions:
start Start Caddis at http://localhost:<port>
stop Stop a previously started Caddis daemon
options:
-h Display this help menu
-p Desired port to start server on localhost
$ caddis start
$ curl -X POST -d '{"method":"GET", "uri":"/foo", "response":{"bar":"baz"}}' http://localhost:3001/api --header "Content-Type:application/json"
Visit http://localhost:3001/foo, prints:
{
bar: 'baz'
}
$ caddis stop
Visit http://localhost:3001/foo, Not Found.
A caddis is a moth-like insect often used as models for fly lures in fishing.
The caddis CLI tool is used to start and stop a RESTful JSON service with the ability to POST route configuration and responses, on-the-fly, for mocking and testing purposes.
It may be a stretch, but there's wit in there somewhere...
There are other projects I have been a part of, such as madmin, that allow for dynamically creating RESTful APIs through a User Interface and allows for persistance through I/O.
Recently, I was involved with mocking a service layer for unit testing purposes and found that the manual curation of such an API was too tedious for the task at hand - I wanted the process to be much more fluid and simple.
In this particular instance I needed to:
- Start a server in setup/before
- Dynamically add a route to the service with mock JSON response
- Run the test
- Shut down the server in teardown/after
Fairly simple, and most of all I didnt want any artifacts lying around - in other words I didn't need for any routes that I dynamically created to stick around on my local disk after the tests were done.
As such, caddis was born.
As mentioned briefly above, caddis is a CLI tool. It is recommended to install globally:
$ npm install -g caddis
: you may need to sudo
Once installed, you can start the service (currently defaults to http://localhost:3001) and begin POSTing route configurations in JSON. Here is an example using cUrl that dynamically adds a GET route at /foo
with a simple JSON payload of {"bar":"baz"}
:
$ curl -X POST -d '{"method":"GET", "uri":"/foo", "response":{"bar":"baz"}}' http://localhost:3001/api --header "Content-Type:application/json"
You are not confiuned to cUrl - you can use whatever networking library in whatever language you are writing your tests in and the server can handle all modern RESTful methods:
- GET
- POST
- PUT
- DELETE
When you are finished, simply stop the caddis server:
$ caddis stop
The post body data that is sent to caddis has the following properties:
- method: The REST method
- uri: The endpoint of the service to hit (requires prepended '/')
- response: The JSON object to return on request
- statusCode: [Optional] Status code to return. Defaults to 200
- delay: [Optional] Time delay, in milliseconds, to delay response. Defaults to NaN
$ npm run test
It is also possible to use caddis
as a locally required module (as oppose to installing globally with -g
):
$ npm install caddis
var caddis = require('caddis');
caddis.start(3002, function() {
// caddis started on http://localhost:3002
});
...
caddis.stop(function() {
// caddis stopped.
});
$ npm run test-module
Copyright (c) 2014 Todd Anderson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.