0.4.6 • Published 7 years ago

mock-api-server v0.4.6

Weekly downloads
260
License
MIT
Repository
github
Last release
7 years ago

mock-api-server

A flexible and powerful stand-in API server.

This server is meant to be booted quickly (for example, inside a test suite) in a Node.js process.

Booting

From the command-line:

./node_modules/.bin/mock-api-server --port PORT

To boot once for a test:

var MockApi = require('mock-api-server');
var api = new MockApi({"port": 7000});
api.start(function(err) {
  // ... do stuff ...
  api.stop();
})

To connect to an existing server:

var MockApi = require('mock-api-server');
var api = new MockApi({"port": 7000});
api.reset(); // Or whatever you want to do.

See test/server_test.coffee for more detailed examples.

If you are using Mocha, you can also boot the server in a before clause. It's also possible to boot the server once at the beginning of the test suite.

Options

The MockApi supports the following options:

Canned Responses

Canned responses live in your project's test/mock-api directory. This directory and its subdirectories has the same structure as your API. For example, to serve an endpoint /v2/foobizzle, populate the file test/mock-api/GET/v2/foobizzle.json.

Responding to HTTP Methods

Files in the test/mock-api/GET subdirectory are used for GET requests. Files in test/mock-api/PUT subdirectory are used for PUT requests, and so forth.

Responding to Query Parameters

If you have these three files:

test/mock-api/GET/v2/foobizzle.json
test/mock-api/GET/v2/foobizzle.json?type=search
test/mock-api/GET/v2/foobizzle.json?type=search&s=foo

then mock-api-server will serve the third one when type=search and s=foo are provided as query parameters. If only type=search is provided, the second one will be served--mock-api-server will take the most specific matching file.

* can be used to match zero or more characters. For example, the following file:

test/mock-api/GET/v2/foobizzle.json?type=*search*

Will match requests with a query parameter type containing a value "search" or "index,search" or "search,index".

Note that most shells will interpret ?, *, and &, so to create these files, you will have to backslash them. For example:

$ touch test/mock-api/GET/v2/foobizzle.json\?type=\*search\*\&s=foo

Live Responses

You can tell the API server to respond to a particular request like so:

api.respondTo('/foo/bar').with({status: 'OK', headers: {'access-control-allow-origin': '*'}});

This will be active until the next time api.reset() is called.

You can modify an existing response with:

api.respondTo('/foo/bar').byReplacing('foo.bar[1].baz').with([ 76 ]);
0.4.6

7 years ago

0.4.5

7 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.2

10 years ago