DummyApi

API

class DummyApi(opts)

A dummy of the sandbox’s real api for use tests and demos.

Arguments:
static config

The api’s DummyConfigResource().

static contacts

The api’s DummyContactsResource().

static groups

The api’s DummyGroupsResource().

static http

The api’s DummyHttpResource().

static kv

The api’s DummyHttpResource().

static log

The api’s DummyLogResource().

static metrics

The api’s DummyMetricsResource().

static outbound

The api’s DummyOutboundResource().

class DummyLogResource(name)

Handles api requests to the log resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
static critical

An array of the messages logged at the 'CRITICAL' log level

static debug

An array of the messages logged at the 'DEBUG' log level

static error

An array of the messages logged at the 'CRITICAL' log level

static info

An array of the messages logged at the 'INFO' log level

static store

An object mapping log levels to the messages logged at that level.

static warning

An array of the messages logged at the 'WARNING' log level

class DummyConfigResource(name)

Handles api requests to the config resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
static app

A shortcut to DummyConfigResource.store.config (the app’s config).

static json

An object specifying which keys in store should be serialized to JSON when being retrieved using 'config.get'. The default for keys not listed is true.

static store

An object containing the sandbox’s config data. Properties do not need to be JSON-stringified, this is done when the config is retrieved using a 'config.get' api request.

class DummyHttpResource(name, opts)

Handles api requests to the http resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
  • opts.default_encoding (string) – The encoding to use for encoding requests and decoding responses. Possible values are 'json' and 'none'. If a request’s Content-Type header is set, the encoding is inferred using that instead.
static fixtures

The resource’s fixture set to use to send out responses to requests. See HttpFixtures().

static requests

A list of http requests that have been sent to the resource, where each is of type HttpRequest().

class HttpFixture(opts)

Encapsulates an expected http request and the responses that be sent back.

Arguments:
  • opts.request.url (string or RegExp()) – The request url. If a string is given, the url may include params. If the params are included, these will be decoded and set as the HttpRequest()‘s params.
  • opts.request.method (string) – The request method. Defaults to ‘GET’.
  • opts.request.data (object) – The request’s un-encoded body data. Optional.
  • opts.request.body (object) – The request’s already encoded body data. Optional.
  • opts.request.params (object or array) – An object of key-value pairs to append to the URL as query parameters. Can be in any form accepted by node.js’s querystring module
  • opts.request.headers (object) – An object mapping each header name to an array of header values.
  • opts.response (object) – A single response to use for this fixture, for cases where one request is sent out.
  • opts.response.code (integer) – The response’s status code
  • opts.response.data (object) – The responses’s decoded body data. Optional.
  • opts.response.body (object) – The response’s un-decoded body data. Optional.
  • opts.responses (array) – An array of response data objects to use one after the other each time a new request is sent out.
  • opts.repeatable (boolean) – Configures the fixture’s response to be reused for every new request. Defaults to false.
  • opts.default_encoding (string) – The encoding to use for encoding requests and decoding responses. Possible values are ‘json’ and ‘none’. If the request’s ‘Content-Type’ header is set, the encoding is inferred using that instead.

Either opts.response or opts.responses can be specified, or neither, but not both. If no responses are given, an ‘empty’ response with a status code of 200 is used.

static use()

Returns the fixture’s next unused HttpResponse().

class HttpFixtures(opts)

Manages a set of HttpFixture() instances.

Arguments:
  • opts.match (function) – A function of the form f(request, fixture), where request is the request that needs a match, and fixture is the current HttpFixture() being tested as a match. Should return true if the request and fixture match or false if they do not match.
  • opts.defaults (boolean) – Defaults to use for each added fixture.
  • opts.default_encoding (string) – The encoding to use for encoding requests and decoding responses. Possible values are ‘json’ and ‘none’. If a request’s ‘Content-Type’ header is set, the encoding is inferred using that instead.
static add(data)

Adds an http fixture to the fixture set from raw data.

Arguments:
  • data (object) – The properties of the fixture to be added. See HttpFixture().
Returns:

The HttpFixture() that was created.

static filter(request)

Finds the fixtures that match the given request.

Arguments:
  • request (HttpRequest) – The request to find a match for.
class DummyContactsResource(name)

Handles api requests to the contacts resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
static add(contact)

Adds an already created contact to the resource’s store.

Arguments:
  • contact (Contact) – The contact to add.
static add(attrs)

Adds an contact to the resource via a data object.

Arguments:
  • attrs (object) – The attributes to initialise a contact with.
static search_results

An object mapping expected search queries to an array of the matching keys.

static store

A list of the resource’s currently stored contacts.

class DummyGroupsResource(name)

Handles api requests to the groups resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
  • contacts (DummyContactsResource) – The contacts resource associated to this groups resource.
static add(group)

Adds an already created group to the resource’s store.

Arguments:
  • group (Group) – The group to add.
static add(attrs)

Adds an group to the resource via a data object.

Arguments:
  • attrs (object) – The attributes to initialise a group with.
static search_results

An object mapping expected search queries to an array of the matching keys.

static store

A list of the resource’s currently stored groups.

class DummyKvResource(name[, store])

Handles api requests to the kv resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
  • store (object) – The data to initialise the store with.
static incr(key[, amount])

Increment the value of an integer key. The current value of the key must be an integer. If the key does not exist, it is set to zero. Returns the result.

Arguments:
  • key (string) – The key corresponding to the value to increment
  • amount (integer) – The amount to increment by. Defaults to 1.
static set_ttl(key[, seconds])

Set or remove the ttl (expiry time) of a key.

If seconds is null or undefined the key is set not to expire (and its ttl is removed).

Arguments:
  • key (string) – The key to set the ttl for.
  • seconds (integer) – The number of seconds to set the ttl to. Defaults to null.
static store

An object mapping all the keys in the store to their corresponding values.

static ttl

An object mapping keys set to expire to their lifetime (in seconds).

class DummyMetricsResource(name)

Handles api requests to the metrics resource from DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests.
static add(metric)

Records a fired metric.

Arguments:
  • data.store (string) – the name of the metric
  • data.metric (string) – the name of the metric
  • data.agg (string) – the name of the aggregation method
  • data.value (number) – the value to store for the metric
static agg

The aggregation method for metrics with the name metric_name that have been fired to the store with the name store_name.

static values

An array of the metric values for metrics with the name metric_name that have been fired to the store with the name store_name.

class DummyOutboundResource(name)

Handles api requests to the outbound resource from DummyApi().

Arguments:
static store

An array of the sent outbound message objects.

Under the Hood

class DummyResource(name)

A resource for handling api requests sent to a DummyApi().

Arguments:
  • name (string) – The name of the resource. Should match the name given in api requests (for eg, name would be 'http' for http.get api request).
static handle(cmd)

Handles an api request by delegating to the resource handler that corresponds to cmd.

Arguments:
  • cmd (object) – The api request command to be handled.
static handlers

An object holding the resource’s handlers. Each property name should be the name of the resource handler used in api requests (for eg, 'get' for 'http.get'), and each property value should be a function which accepts a command and returns an api result. For eg:

self.handlers.foo = function(cmd) {
    return {
        success: true,
        bar: 'baz'
    };
};
class DummyResources()

Controls a DummyApi()‘s resources and delegates api requests to correspinding resource. *

static add(resource)

Adds a resource to the resource collection.

Arguments:
static attach(api)

Attaches the resource collection’s resources directly onto a DummyApi(). Simply a convenience to provide users with direct access to the resource.

Arguments:
  • api (DummyApi) – the api to attach to
static get(name)

Returns a resource by name

Arguments:
  • name (string) – The name of the resource
static handle(cmd)

Handles an api request by delegating to the corresponding resource.

Arguments:
  • cmd (object) – The api request command to be handled.
static has_resource_for(cmd)

Determines whether the resource collection has a corresponding resource for cmd.

Arguments:
  • cmd (object) – The command to look for a resource for.