2

自学mountebank。我想创建一个模拟端点,以便通过向http://localhost:2525/test?mock-response-code=500发出 POST 命令,响应代码将为 500,消息正文将是一些自定义文本。

但是,当我创建冒名顶替者并运行相关的 curl 命令时,我得到了 200 响应。

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} 
       }
    }]
  }]
}

以下是我尝试使用该 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

这是一些调试输出:

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

(端口与上面的 b/c 不同,它来自不同的运行。)

4

1 回答 1

1

我自己通过不断的实验找到了答案。

引号——就这么简单。

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

而不是

           "query": {"mock-response-code": 400}
于 2019-08-19T08:53:02.810 回答