A library-agnostic API mocking library.
It's extremely lightweight, flexible and follows UMD.
bower install --save-dev demock
Or:
npm install --save-dev demock
- Hook into your application's HTTP transport to intercept requests.
- Pass a request through demock, which in turn passes it through all the configured request filters.
- Pass the filtered request down to the HTTP transport to make the actual HTTP request.
- 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.
- Pass the fitered response up to the application.
- Include Demock
- Include a transport adaptor
- Enable some filters
Two transport adaptors are readily available:
Get a new demock instance:
var demock = new Demock();
Adds a filter to the request filter chain. A filter is a function that takes in a request object:
demock.appendRequestFilter(function (request) {
// manipulate the request
});
Adds a filter to the response filter chain. A filter is a function that takes in a request and a response object:
demock.appendResponseFilter(function (request, response) {
// manipulate the request and response
});
Runs all request filters on an abstract request object:
demock.filterRequest(request);
Runs all response filters on an abstract response object. The abstract request object that's used for .filterRequest()
is used again:
demock.filterResponse(request, response);
An abstract representation of 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.
- headers
- The request headers. This is an object.
{
method: 'GET',
url: '/api/users',
params: { id: 1, sortKey: 'name' },
headers: { 'X-Custom': 'foo' }
}
An abtract representation of 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).
- headers
- The response headers. This is an object.
{
statusCode: 200,
statusText: 'OK',
data: [{ name: 'John' }, { name: 'Jane' }],
headers: { 'Content-Type': 'application/json' }
}
Demock comes with stock request filters as properties of the Demock.requestFilters
object.
- Changes the method of non-GET requests to GET
- Appends the original method name to the path
- Includes the original request parameters as "X-Request-*" HTTP headers
demock.appendRequestFilter(Demock.requestFilters.method());
var request = {
method: 'POST',
url: '/api/foo',
params: {
a: 1,
b: 2
}
};
demock.filterRequest(request);
/* request becomes:
{
method: 'GET',
url: '/api/foo/POST',
params: {
a: 1,
b: 2
}
headers: {
'X-Request-Param-a': 1,
'X-Request-Param-b': 2
}
}
*/
Appends a default document to the path. This is useful when you don't want to configure your static web server with a custom default document (i.e. something other than index.html, etc.)
The filter instance expects a configuration object with the defaultDocument
property.
demock.appendRequestFilter(Demock.requestFilters.defaultDocument({
defaultDocument: 'data.json'
});
var request = {
method: 'GET',
url: '/api/foo'
};
demock.filterRequest(request);
/* request becomes:
{
method: 'GET',
url: '/api/foo/data.json'
}
*/
The only response filtering that is built-in is the data replacement filter. Response filters can make use of this filter for nested application of response filters on response data.
var response = {
data: {
$data: {
a: 1
}
}
};
demock.filterResponse(request, response);
/* response becomes:
{
data: {
a: 1
}
}
*/
Demock comes with stock response filters as properties of the Demock.responseFilters
object.
Sets the delay
property of the response object to simulate latency.
demock.appendResponseFilter(Demock.responseFilters.delay());
var response = {
data: {
$delay: 250,
$data: {
a: 1
}
}
};
demock.filterResponse(request, response);
/* response becomes:
{
delay: 500,
data: {
a: 1
}
}
*/
Overrides the response status code and text.
demock.appendResponseFilter(Demock.responseFilters.status());
var response = {
statusCode: 200,
statusText: 'OK',
data: {
$status: {
code: 500,
text: 'Internal Server Error'
},
$data: {
a: 1
}
}
};
demock.filterResponse(request, response);
/* response becomes:
{
statusCode: 500,
statusText: 'Internal Server Error',
data: {
a: 1
}
}
*/
A typical transport adaptor would do:
// Intercept request
// ...
// Compose abstract request object from original request configuration:
var request = {
method: httpConfig.method,
url: httpConfig.url,
params: httpConfig.params,
headers: httpConfig.headers
};
// Run request filters:
demock.filterRequest(request);
// Convey changes from the abstract request to the real request configuration and perform the request:
httpConfig.method = request.method;
httpConfig.url = request.url;
httpConfig.params = request.params;
httpConfig.headers = request.headers;
// Perform request
// ...
// Compose abstract response object from original response object:
var response = {
statusCode: httpResponse.statusCode,
statusText: httpResponse.statusText,
headers: httpResponse.headers,
data: httpResponse.data
};
// Run response filters:
demock.filterResponse(request, response);
// Convey changes from the abstract response to the real response:
httpResponse.statusCode = response.statusCode;
httpResponse.statusText = response.statusText;
httpResponse.headers = response.headers;
httpResponse.data = response.data;
// Return modified response
// ...