elbow
An easy way to test REST API responses with Mocha
elbow = mocha + superagent + jayschema + awesomeness
what makes it easy?
- you only write one, short test file with one test suite
- your schemas define what endpoints they are tested against
- it fits just right in your work flow
usage:
A sample test script:
var elbow = require("elbow");
describe("testing Http Responses", function() {
elbow.run(it, "http://localhost:9090/", __dirname + "/../schema");
});
See a sample schema.
See a sample test output.
installation:
⇒ npm install elbow --save-dev
API:
var elbow = require("elbow");
elbow.run(it, baseUrl, schemaDir)
Runs your tests.
it
(Function): it provided by Mocha.baseUrl
(String): base url of the server. This is used to resolve the relative urls (endpoints).schemaDir
(String): path to the directory holding your schemas.
elbow.schemas(schemaDir, callback)
Loads your schemas.
schemaDir
(String): path to the directory holding your schemas.callback
(Function):
- signature:
callback(err, schemas)
schemas
: object holding your schemas whose keys are the various endpoints
schemas:
All schemas should be placed in a single directory. They should be valid JSON documents with the extension .json
.
A sample schema file would look like:
{
"$schema": "http://json-schema.org/schema#",
"endpoint": "/transactions/transfers/charges",
"description": "transfer charges",
"methods": ["get", "post"],
"params": {
"to": "registered",
"amount": 5000
},
"type": "object",
"properties": {
"charge": {
"type": "string"
}
},
"required": ["charge"]
}
Required key-value pairs include:
endpoint
(String): endpoint to test. This will be resolved to an absolute url using a base url.description
(String): describes the significance of the Http response.methods
(Array): all the Http methods to use to test the endpoint
- possible values:
"get"
, "post"
, "put"
params
(Object): parameters to pass to endpoint.
The rest of the document will be used as is in validation.
test output:
Sample Test Output:
✓ GET /params (testing the sending of params) [params] (107ms)
✓ POST /params (testing the sending of params) [params] (92ms)
✓ PUT /params (testing the sending of params) [params]
✓ GET /simple (testing the response body only) [simple]
✓ POST /simple (testing the response body only) [simple]
✓ PUT /simple (testing the response body only) [simple]
The method and endpoint is shown for each test case. The description of the schema is shown between (
and )
. The filename (without the extension) of the schema used in the test case is shown between [
and ]
.
terminal usage:
Elbow is also available from your terminal.
If installed globally, the command elbow
will be readily available. Otherwise, elbow will be available at ./node_modules/.bin/elbow
.
listing your schemas:
To list your schemas with the respective descriptions.
⇒ elbow --list [pathToSchemaDir]
license:
The MIT License (MIT)
Copyright (c) 2015 GochoMugo mugo@forfuture.co.ke