predicatestubmountebank

Why does Mountebank predicate cause 200 instead of 500 in response?


Teaching self mountebank. I want to create a mock endpoint such that by issuing POST command to http://localhost:2525/test?mock-response-code=500, the response code will be 500 and the message body will be some custom text.

When I create the imposter, however, and run the relevant curl command, I am getting a 200 response.

curl -i -X POST http://localhost:2525/imposters --data '@createImposter_r500.ejs'
info: [mb:2525] POST /imposters
info: [http:61161] Open for business...
HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Location: http://localhost:2525/imposters/61161
Content-Type: application/json; charset=utf-8
Content-Length: 767
Date: Sun, 18 Aug 2019 13:57:57 GMT
Connection: keep-alive

{
  "protocol": "http",
  "port": 61161,
  "numberOfRequests": 0,
  "recordRequests": false,
  "requests": [],
  "stubs": [
    {
      "responses": [
        {
          "is": {
            "statusCode": 500
          }
        }
      ],
      "predicates": [
        {
          "equals": {
            "path": "/test",
            "method": "POST",
            "query": {
              "mock-response-code": 500
            }
          }
        }
      ],
      "_links": {
        "self": {
          "href": "http://localhost:2525/imposters/61161/stubs/0"
        }
      }
    }
  ],
  "_links": {
    "self": {
      "href": "http://localhost:2525/imposters/61161"
    },
    "stubs": {
      "href": "http://localhost:2525/imposters/61161/stubs"
    }
  }
}

createImposter_r500.ejs:

{
  "protocol": "http",
  "stubs": [{
    "responses": [
      { "is": { "statusCode": 500 }}
    ],
    "predicates": [{
          "equals": {
            "path": "/test",
            "method": "POST",
        "query": {"mock-response-code": 500} 
       }
    }]
  }]
}

Here is what happens when I try using that URL:

curl -i -X POST http://localhost:61161/test?mock-response-code=500
info: [http:61161] ::1:61429 => POST /test?mock-response-code=500
HTTP/1.1 200 OK
Connection: close
Date: Sun, 18 Aug 2019 14:18:06 GMT
Transfer-Encoding: chunked

Here is some debug output:

debug: [http:62650] ::1:63139 ESTABLISHED
info: [http:62650] ::1:63139 => POST /test?mock-response-code=400
debug: [http:62650] ::1:63139 => {"requestFrom":"::1:63139","method":"POST","path":"/test","query":{"mock-response-code":"400"},"headers":{"Host":"localhost:62650","User-Agent":"curl/7.54.0","Accept":"*/*"},"body":"","ip":"::1"}
debug: [http:62650] no predicate match
debug: [http:62650] generating response from {"is":{}}
debug: [http:62650] ::1:63139 <= {"statusCode":200,"headers":{"Connection":"close"},"body":"","_mode":"text"}
debug: [http:62650] ::1:63139 CLOSED

(Port is different from above b/c it's from a different run.)


Solution

  • I found the answer myself with continued experimentation.

    Quotation marks - that simple.

               "query": {"mock-response-code": "400"} 
    

    rather than

               "query": {"mock-response-code": 400}