<% title = 'behaviors' description = 'Additional behaviors supported by all mountebank responses' %> <%- include('../../_header') -%>

Stub Behaviors

You can alter the response created by adding to the behaviors array, which acts as a middleware pipeline of transformations to the response. At the moment, mountebank accepts the following behaviors:

Behavior Description
wait Adds latency to a response by waiting a specified number of milliseconds before sending the response.

Tip: Setting the addWaitBehavior flag on proxies will automatically add this behavior with the actual time it took to call the downstream service

copy Copies one or more values from request fields into the response. You can tokenize the response and select values from request fields using regular expressions, xpath, or jsonpath.
lookup Queries an external data source for data based on a key selected from the request. Like the copy behavior, you can tokenize the response and select the key from the request using regular expressions, xpath, or jsonpath.
decorate Post-processes the response using JavaScript injection before sending it. Post-processing opens up a world of opportunities - you can use a decorate behavior to add data to a proxied response or substitute data from the request into the response, for example. The value passed into the decorate behavior is a JavaScript function that can take up to three values: the request, the response, and a logger. You can either mutate the response passed in (and return nothing), or return an altogether new response.

Tip: Setting the addDecorateBehavior flag on proxies will automatically add this function as decorate behavior on the generated responses

The --allowInjection command line flag must be set to support this behavior

shellTransform Like decorate, a shellTransform post-processes the response, but instead of using JavaScript injection, it shells out to another application. That application will get two command line parameters representing the request JSON and the response JSON, and should print to stdout the transformed response JSON.

The --allowInjection command line flag must be set to support this behavior.

Multiple behaviors can be added to a response, and they will be executed in array order. While each object in the array may contain only one type of behavior, you are free to repeat any behavior as many times as you want. For example, take a look at the following response:

{
  "is": " { ... },
  "behaviors": [
    { "copy": { ... } },
    { "decorate": "..." },
    { "lookup": "..." },
    { "shellTransform": "..." },
    { "decorate": "..." },
    { "wait": 500 },
    { "shellTransform": "..." }
  ]
}

The ability to compose multiple behaviors together gives you complete control over the response transformation.

Examples

Select the behavior below for relevant examples, which explore each type of behavior in isolation:

wait
<%- include('behaviors/wait') -%>
copy
<%- include('behaviors/copy') -%>
lookup
<%- include('behaviors/lookup') -%>
decorate
<%- include('behaviors/decorate') -%>
shellTransform
<%- include('behaviors/shellTransform') -%>
<%- include('../../_footer') -%>