bbyars/mountebank

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

Summary

Maintainability
Test Coverage
<p>Let's create a binary-based imposter with multiple stubs:</p>

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

{
  "port": 4549,
  "protocol": "tcp",
  "mode": "binary",
  "stubs": [<strong class='highlight1'>
    {
      "responses": [{ "is": { "data": "Zmlyc3QgcmVzcG9uc2U=" } }],
      "predicates": [{ "endsWith": { "data": "AwQ=" } }]
    }</strong>,<strong class='highlight2'>
    {
      "responses": [{ "is": { "data": "c2Vjb25kIHJlc3BvbnNl" } }],
      "predicates": [{ "endsWith": { "data": "BQY=" } }]
    }</strong>,<strong class='highlight3'>
    {
      "responses": [{ "is": { "data": "dGhpcmQgcmVzcG9uc2U=" } }],
      "predicates": [{ "endsWith": { "data": "BQY=" } }]
    }</strong>
  ]
}</code></pre>
    </step>

<p>We'll use the command line <code>base64</code> tool to decode the
request to binary before sending to the imposter.  We're sending a
base64-encoded version of four bytes: 0x1, 0x2, 0x3, and 0x4.  Our
first predicate is a base64 encoded version of 0x3 and 0x4.  The
response is a base64-encoded version of the string "first response":</p>

    <step type='exec'>
<pre><code>echo '<strong class='highlight1'>AQIDBA==</strong>' | base64 --decode | nc localhost 4549</code></pre>

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

<p>Next we'll send 0x1, 0x2, 0x4, 0x5, and 0x6, matching on a predicate
encoding 0x5 and 0x6:</p>

    <step type='exec'>
<pre><code>echo '<strong class='highlight2'>AQIDBAUG</strong>' | base64 --decode | nc localhost 4549</code></pre>

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

<p>The third stub will never run, since it matches the same requests as the
second 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/4549 HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json</code>
    </step>
</testScenario>