bbyars/mountebank

View on GitHub
src/views/docs/api/behaviors.ejs

Summary

Maintainability
Test Coverage
<%
title = 'behaviors'
description = 'Additional behaviors supported by all mountebank responses'
%>

<%- include('../../_header') -%>

<h1>Stub Behaviors</h1>

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

<table id='behaviors'>
  <tr>
    <th>Behavior</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><code>wait</code></td>
    <td>Adds latency to a response by waiting a specified number of milliseconds before sending
      the response.

      <p class='info-icon'>Tip: Setting the <a href='/docs/api/proxies'><code>addWaitBehavior</code></a>
      flag on proxies will automatically add this behavior with the actual time it took to call the
      downstream service</p>
    </td>
  </tr>
  <tr>
    <td><code>copy</code></td>
    <td>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.</td>
  </tr>
  <tr>
    <td><code>lookup</code></td>
    <td>Queries an external data source for data based on a key selected from the request. Like the
    <code>copy</code> behavior, you can tokenize the response and select the key from the request
    using regular expressions, xpath, or jsonpath.</td>
  </tr>
  <tr>
    <td><code>decorate</code></td>
    <td>Post-processes the response using JavaScript injection before sending it. Post-processing opens
      up a world of opportunities - you can use a <code>decorate</code> behavior to add data to a proxied
      response or substitute data from the request into the response, for example.  The value passed into
      the <code>decorate</code> 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.

      <p class='info-icon'>Tip: Setting the <a href='/docs/api/proxies'><code>addDecorateBehavior</code></a>
      flag on proxies will automatically add this function as decorate behavior on the generated responses</p>

      <p class='warning-icon'>The <code><a href='/docs/commandLine'>--allowInjection</a></code> command
      line flag must be set to support this behavior</p>
    </td>
  </tr>
  <tr>
    <td><code>shellTransform</code></td>
    <td>Like <code>decorate</code>, a <code>shellTransform</code> 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 <code>stdout</code> the transformed response JSON.

    <p class='warning-icon'>The <code><a href='/docs/commandLine'>--allowInjection</a></code> command
    line flag must be set to support this behavior.</p>
    </td>
  </tr>
</table>

<p>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:</p>

<pre><code>{
  "is": " { ... },
  "behaviors": [
    { "copy": { ... } },
    { "decorate": "..." },
    { "lookup": "..." },
    { "shellTransform": "..." },
    { "decorate": "..." },
    { "wait": 500 },
    { "shellTransform": "..." }
  ]
}</code></pre>

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

<h2>Examples</h2>

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

<section class='accordion'>
  <div>
    <a class='section-toggler'
       id='behavior-wait' name='behavior-wait' href='#behavior-wait'>
      wait
    </a>
    <section>
      <%- include('behaviors/wait') -%>
    </section>
  </div>
  <div>
    <a class='section-toggler'
       id='behavior-copy' name='behavior-copy' href='#behavior-copy'>
      copy
    </a>
    <section>
      <%- include('behaviors/copy') -%>
    </section>
  </div>
  <div>
    <a class='section-toggler'
       id='behavior-lookup' name='behavior-lookup' href='#behavior-lookup'>
      lookup
    </a>
    <section>
      <%- include('behaviors/lookup') -%>
    </section>
  </div>
  <div>
    <a class='section-toggler'
       id='behavior-decorate' name='behavior-decorate' href='#behavior-decorate'>
      decorate
    </a>
    <section>
      <%- include('behaviors/decorate') -%>
    </section>
  </div>
  <div>
    <a class='section-toggler'
       id='behavior-shellTransform' name='behavior-shellTransform' href='#behavior-shellTransform'>
      shellTransform
    </a>
    <section>
      <%- include('behaviors/shellTransform') -%>
    </section>
  </div>
</section>

<%- include('../../_footer') -%>