bbyars/mountebank

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

Summary

Maintainability
Test Coverage
<p>The <code>exists</code> predicate is primarily for object data types, like
HTTP headers and query parameters.  It works on string fields by simply returning
<code>true</code> if the <code>exists</code> value is <code>true</code> and the
string if non-empty.  Setting the <code>exists</code> value to <code>false</code>
inverts the meaning.</p>

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

{
  "port": 4551,
  "protocol": "http",
  "stubs": [<strong class='highlight1'>
    {
      "responses": [{ "is": { "body": "first response" } }],
      "predicates": [
        {
          "exists": {
            "query": {
              "q": true,
              "search": false
            },
            "headers": {
              "Accept": true,
              "X-Rate-Limit": false
            }
          }
        }
      ]
    }</strong>,<strong class='highlight2'>
    {
      "responses": [{ "is": { "body": "second response" } }],
      "predicates": [
        {
          "exists": {
            "method": true,
            "body": false
          }
        }
      ]
    }</strong>,<strong class='highlight3'>
    {
      "responses": [{ "is": { "body": "third response" } }],
      "predicates": [
        {
          "exists": { "body": true }
        }
      ]
    }</strong>
  ]
}</code></pre>
    </step>

<p>The first stub matches if the querystring includes <code>q</code>, but not if it
includes <code>search</code>, and if the headers include <code>Accept</code>,
but not if they include <code>X-Rate-Limit</code>.</p>

    <step type='http'>
<pre><code>GET /?<strong class='highlight1'>q</strong>=mountebank HTTP/1.1
Host: localhost:4551
<strong class='highlight1'>Accept</strong>: text/plain</code></pre>

        <assertResponse>
<pre><code>HTTP/1.1 200 OK
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked

<strong class='highlight1'>first response</strong></code></pre>
        </assertResponse>
    </step>

<p>The second stub matches if the request <code>method</code> is a
non-empty string (always <code>true</code>), and if the <code>body</code>
is empty.</p>

    <step type='http'>
<pre><code>GET / HTTP/1.1
Host: localhost:4551</code></pre>

        <assertResponse>
<pre><code>HTTP/1.1 200 OK
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked

<strong class='highlight2'>second response</strong></code></pre>
        </assertResponse>
    </step>

<p>The last stub matches if the <code>body</code> is non-empty:</p>

    <step type='http'>
<pre><code>POST / HTTP/1.1
Host: localhost:4551

<strong class='highlight3'>non-empty body</strong></code></pre>

        <assertResponse>
<pre><code>HTTP/1.1 200 OK
Connection: close
Date: <volatile>Thu, 09 Jan 2014 02:30:31 GMT</volatile>
Transfer-Encoding: chunked

<strong class='highlight1'>third response</strong></code></pre>
        </assertResponse>
    </step>

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