mountebank

mountebank - over the wire test doubles


the apothecary

http

Imposter Creation Parameters

Parameter Options Required? Default Description
protocol http Yes N/A  
port Any valid port number No A randomly assigned port. mountebank will return the actual value in the POST response. The port to run the imposter on.
name Any string No empty string Included in the logs, useful when multiple imposters are set up.
recordRequests true or false No false Adds mock verification support by remembering the requests made to this imposter. Note that this represents a memory leak for any long running mb process, as requests are never forgotten.
stubs Valid stubs No An empty array The list of stubs responsible for matching a request and returning a response
defaultResponse A valid response, see below for response fields No

{
  "statusCode": 200,
  "headers": {
    "connection": "close"
   },
  "body": ""
}
    
The default response to send if no predicate matches. Also represents the default values that get merged into a response that doesn't specify every field
allowCORS boolean No false If true, mountebank will allow all CORS preflight requests on the imposter.

http and https imposters prevent keepalive connections by default because they can lead to some difficult to troubleshoot problems in use cases where you start in stop imposters in the scope of one test. The test may shut down the imposter, which prevents new connections for the port, but that won't prevent the system under test from trying to reuse an existing keepalive socket. If you need to override that behavior, override the defaultResponse field and explicitly set the Connection header to keep-alive.

HTTP Requests

Field Description Type
requestFrom The client socket, primarily used for logging and debugging. string
path The path of the request, without the querystring string
query The querystring of the request object
method The request method string
headers The HTTP headers object
body The request body string
form Form-encoded key-value pairs in the body. Supports key-specific predicates. For example, with a body of firstname=bob&lastname=smith, you could set a predicate on just lastname. object

HTTP Responses

Field Type Default
statusCode int 200
headers object { "Connection": "close" }
body string or object ""
_mode string - binary or text text

While HTTP bodies are strings, you can pass a JSON body in the API. That will be turned into a valid JSON string when the response is sent.

HTTP bodies will always be recorded as text, but mountebank does have the ability to respond in binary. If you want to set up a canned binary response, set the _mode to binary and base64 encode the body. mountebank will also try to preserve binary responses in proxies by looking at the Content-Encoding and Content-Type headers.

Inline JSON For Response Bodies

The example below shows passing an inline JSON object as the response body.

POST /imposters HTTP/1.1
Host: localhost:35553
Accept: application/json
Content-Type: application/json

{
  "port": 4545,
  "protocol": "http",
  "stubs": [
    {
      "responses": [
        {
          "is": {
            "statusCode": 200,
            "headers": {
              "Content-Type": "application/json"
            },
            "body": {
              "bikeId": 123,
              "name": "Turbo Bike 4000"
            }
          }
        }
      ]
    }
  ]
}

Now let's test the response by calling the imposter:

GET / HTTP/1.1
Host: localhost:4545
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Connection: close
Date: Sun, 15 Nov 2015 01:02:03 GMT
Transfer-Encoding: chunked

{
  "bikeId": 123,
  "name": "Turbo Bike 4000"
}