Skip to content
Alistair Dutton edited this page Mar 15, 2017 · 8 revisions

You can find examples of the API calls in test/e2e/vanilli-test.js.

vanilli

The functions below operate on the vanilli instance itself.

vanilli.stub

vanilli.stub(stub1, stub2, ... , stubX);

Registers one or more stubs.

vanilli.stubDefault

vanilli.stubDefault(stub1, stub2, ... , stubX);

Identical to vanilli.stub except that it assigns a very high priority value to each stub (thus ensuring that the stub(s) ONLY matches if no other stub matches - see below for more info).

vanilli.expect

vanilli.expect(stub1, stub2, ... , stubX);

Registers one or more stubs as expectations. (See 'Stubs vs Expectations' section below.)

vanilli.onGet

vanilli.onGet(url[, options]);

Returns a new stub that will match against an incoming GET request with a relative URL matching that specified. Further matching criteria can be specified via the options object. The following snippet illustrates all available options:

{
    contentType: <string or RegExp>,
    body: <string, object or RegExp>,
    query: {
        param1: <string or RegExp>,
        ...
        paramX: <string or RegExp>
    },
    headers: {
        header1: <string or RegExp>,
        ...
        headerX: <string or RegExp>
    },
    priority: 0,
    times: 2
}

NOTES:

  • If a body is specified then a contentType MUST be specified.
  • The times of the stub indicates the maximum number of times the stub will be matched.
  • The priority of the stub indicates the precedence of the stub over other stubs matching the same request: whichever stub has the lowest priority value "wins". By default a stub has the priority 0.
  • Note the convenience method stubDefault that automatically assigns a very high value to a stub (thus ensuring that it only matches if no other stub matches).

vanilli.onPut

As for vanilli.onGet except that the HTTP method for the stub is PUT.

vanilli.onPost

As for vanilli.onGet except that the HTTP method for the stub is POST.

vanilli.onDelete

As for vanilli.onGet except that the HTTP method for the stub is DELETE.

vanilli.listen

vanilli.listen(port);

Starts the vanilli REST server listening on the specified port.

vanilli.stop

vanilli.stop();

Stops the vanilli REST server.

vanilli.verify

vanilli.verify();

Verifies that the expectations currently registered with vanilli have been met. If verification fails a single error is thrown detailing all unmet expectations.

vanilli.clear

vanilli.clear();

Clears vanilli down of all stubs and expectations.

vanilli.getCaptures

var captureDetails = vanilli.getCaptures(captureId);

Gets details of the requests captured against the specified captureId. (See stub.capture.)

vanilli.getCapture

Legacy version returning just the last capture - use vanilli.getCaptures instead.

stub

The functions below operate on an instance of a stub returned by one of the vanilli.onVERB functions.

stub.capture

stub.capture(captureId);

Indicates that vanilli should store the details of the request that is eventually matched against the stub. The specified captureId can then be used as a handle to vanilli.getCapture to pull those details back for asserting on.

stub.wait

stub.wait(milliseconds);

Indicates that when vanilli matches this stub against an incoming request it should wait the specified number of milliseconds before responding with the stubbed response.

stub.respondWith

stub.respondWith(status[, options]);

Adds details of the response to the stub. At minimum, a status code can be specified; however, more details for the response can be specified via the options. The following snippet illustrates all options.

{
    contentType: <string>,
    body: <string or object>,
    headers: {
        header1: <string>,
        ...
        headerX: <string>
    }
}

NOTE: If a body is specified then a contentType MUST be specified.

stub.anyTimes

If specified (e.g. onGet("/some/url").respondWith(200).anyTimes()) the stub will be matched any number of times against incoming requests. Note that, as you would expect, this has NO effect on expectations.