5

我正在尝试使用独立的wiremock 创建一个 API 模拟。响应正文取决于请求正文中的属性。

使用 JSON,我能够做到。这是示例映射:

{
   "request":{
      "method":"POST",
      "bodyPatterns":[
         {
            "matchesJsonPath":"$.somekey.subkey[?(@.attribute == 'VALUE_123')]"
         }
      ]
   },
   "response":{
      "status":200,
      "bodyFileName":"res.dat",
      "headers":{
         "Content-Type":"application/x-proto;charset=UTF-8"
      }
   }
}

但是,我的主要要求是处理 google protobuf,并且我正在尝试使用文本格式来代替嘲笑者将用来模拟 API 以进行响应的文本格式。因此,请求文件是文本格式,并且没有任何 JSON 验证,如双引号或每行末尾的逗号等。

我发现使用 JSON 路径,由于格式不正确,wiremock 无法匹配请求正文。例如,这样的请求:

{
animal {
type {
key1: "value"
key2: value2
}
}
}

代替

{  
   "animal":{  
      "type":{  
         "key1":"value",
         "key2":"value2"
      }
   }
}

可以说key1=value1应该匹配并且response1.json应该返回,或者当key1=someOtherValueresponse2.json应该返回。是的,key 是 type 的一部分,type 是 animal 的一部分。如何实现此请求正文匹配?

4

1 回答 1

3

你可以这样做:

{
  "request": {
  "method": "POST",
    "url": "/authorize/oauth2/token",
    "bodyPatterns": [ {
          "matches": ".username=(test)&."
      }
    ]
  },
  "response": {
    "status": 200,
    . . .

还有https://github.com/tomakehurst/wiremock/issues/575

于 2017-01-17T08:24:12.723 回答