bbyars/mountebank

View on GitHub
src/views/docs/api/predicates/equals.ejs

Summary

Maintainability
Test Coverage
<p>Let's create an HTTP imposter with multiple stubs:</p>

<testScenario name='http equals example'>
    <step type='http'>
<pre><code>POST /imposters HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json
Content-Type: application/json

{
  "port": 4545,
  "protocol": "http",
  "stubs": [<strong class='highlight1'>
    {
      "responses": [{ "is": { "statusCode": 400 } }],
      "predicates": [
        {
          "equals": {
            "method": "POST",
            "path": "/test",
            "query": {
              "first": "1",
              "second": "2"
            },
            "headers": {
              "Accept": "text/plain"
            }
          }
        },
        {
          "equals": { "body": "hello, world" },
          "caseSensitive": true,
          "except": "!$"
        }
      ]
    }</strong>,<strong class='highlight2'>
    {
      "responses": [{ "is": { "statusCode": 406 } }],
      "predicates": [{ "equals": { "headers": { "Accept": "application/xml" } } }]
    }</strong>,<strong class='highlight3'>
    {
      "responses": [{ "is": { "statusCode": 405 } }],
      "predicates": [{ "equals": { "method": "PUT" } }]
    }</strong>,<strong class='highlight4'>
    {
      "responses": [{ "is": { "statusCode": 500 } }],
      "predicates": [{ "equals": { "method": "PUT" } }]
    }</strong>
  ]
}</code></pre>
    </step>

<p>The first predicate is the most complex, and the request has to match all of the
specified request fields.  We have the option of putting multiple fields under one
<code>equals</code> predicate or splitting each one into a separate predicate in the
array.  In this example, all of the ones that share the default predicate parameters
are together.  For those, neither the case of the keys nor the values will affect the
outcome.  The <code>body</code> predicate is treated separately.  The text will be
compared in a case-sensitive manner, after stripping away the regular expression
<code>!$</code> (an exclamation mark anchored to the end of the string).<p>

<p>The order of the query parameters and header fields does not matter.</p>

    <step type='http'>
<pre><code><strong class='highlight1'>POST /test?Second=2&amp;First=1</strong> HTTP/1.1
Host: localhost:4545
<strong class='highlight1'>accept: text/plain</strong>

<strong class='highlight1'>hello, world!</strong></code></pre>

        <assertResponse>
<pre><code>HTTP/1.1 <strong class='highlight1'>400</strong> Bad Request
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked</code></pre>
        </assertResponse>
    </step>

<p>The second stub matches if the header changes.</p>

    <step type='http'>
<pre><code>POST /test?Second=2&amp;First=1 HTTP/1.1
Host: localhost:4545
<strong class='highlight2'>Accept: application/xml</strong>

"hello, world!"</code></pre>

        <assertResponse>
<pre><code>HTTP/1.1 <strong class='highlight2'>406</strong> Not Acceptable
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked</code></pre>
        </assertResponse>
    </step>

<p>The third stub matches on a <code>PUT</code>.</p>

    <step type='http'>
<pre><code><strong class='highlight3'>PUT</strong> /test?Second=2&amp;First=1 HTTP/1.1
Host: localhost:4545
Accept: application/json

"hello, world!"</code></pre>

        <assertResponse>
<pre><code>
HTTP/1.1 <strong class='highlight3'>405</strong> Method Not Allowed
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked
</code></pre>
        </assertResponse>
    </step>

<p>The fourth stub will never run, since it matches the same requests as the
third stub.  mountebank always chooses the first stub that matches based on
the order you add them to the <code>stubs</code> array when creating the
imposter.</p>

    <step type='http'>
<code class='hidden'>DELETE /imposters/4545 HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json</code>
    </step>
</testScenario>