/demock

Declarative mocking with static JSON

Primary LanguageJavaScript

demock

Build Status

A library-agnostic API mocking library.

How it works

  1. Hook into your application's HTTP transport to intercept requests.
  2. Pass a request through demock, which in turn passes it through all the configured request filters.
  3. Pass the filtered request down to the HTTP transport to make the actual HTTP request.
  4. On receiving the response, pass the response along with the original request through demock, which in turn passes it through all the configured response filters.
  5. Pass the fitered response up to the application.

Two transport adaptors are already available:

  • adaptors/jquery.js
  • adaptors/angular.js

Examples

cd examples
bower install

Then serve the parent directory from a static web server. The serve NPM module is handy:

npm install -g serve
serve ..

API

Properties

.filterPrefix

.config

.requestFilters

.responseFilters

Methods

.filterRequest(request)

.filterResponse(request, response)

Objects

request

An HTTP request. Has the following properties:

method
The request method (uppercase): GET, POST, PUT, DELETE, etc.
url
The request URL.
params
The request parameters. This is an object with key/value pairs as properties.
Example
{
    method: 'GET',
    url: '/api/users',
    params: { id: 1, sortKey: 'name' }
}

response

An HTTP response. Has the following properties:

statusCode
The response status code: 200, 404, etc.
statusText
The response status text: 'OK', 'Not Found', etc.
data
The response payload (Array/Object).
Example
{
    statusCode: 200,
    statusText: 'OK',
    data: [{ name: 'John' }, { name: 'Jane' }]
}