Let's create an HTTP imposter with multiple stubs:
POST /imposters HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json
Content-Type: application/json
{
"port": 4545,
"protocol": "http",
"stubs": [
{
"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": "!$"
}
]
},
{
"responses": [{ "is": { "statusCode": 406 } }],
"predicates": [{ "equals": { "headers": { "Accept": "application/xml" } } }]
},
{
"responses": [{ "is": { "statusCode": 405 } }],
"predicates": [{ "equals": { "method": "PUT" } }]
},
{
"responses": [{ "is": { "statusCode": 500 } }],
"predicates": [{ "equals": { "method": "PUT" } }]
}
]
}
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
equals
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 body
predicate is treated separately. The text will be
compared in a case-sensitive manner, after stripping away the regular expression
!$
(an exclamation mark anchored to the end of the string).
The order of the query parameters and header fields does not matter.
POST /test?Second=2&First=1 HTTP/1.1
Host: localhost:4545
accept: text/plain
hello, world!
HTTP/1.1 400 Bad Request
Connection: close
Date: Thu, 09 Jan 2014 02:30:31 GMT
Transfer-Encoding: chunked
The second stub matches if the header changes.
POST /test?Second=2&First=1 HTTP/1.1
Host: localhost:4545
Accept: application/xml
"hello, world!"
HTTP/1.1 406 Not Acceptable
Connection: close
Date: Thu, 09 Jan 2014 02:30:31 GMT
Transfer-Encoding: chunked
The third stub matches on a PUT
.
PUT /test?Second=2&First=1 HTTP/1.1
Host: localhost:4545
Accept: application/json
"hello, world!"
HTTP/1.1 405 Method Not Allowed
Connection: close
Date: Thu, 09 Jan 2014 02:30:31 GMT
Transfer-Encoding: chunked
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 stubs
array when creating the
imposter.
DELETE /imposters/4545 HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json