WireMock 中的请求正文匹配(不是 JSON/XML)

xpl*_*raj 6 json mocking jsonpath wiremock

我正在尝试使用独立的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"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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

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

{
animal {
type {
key1: "value"
key2: value2
}
}
}
Run Code Online (Sandbox Code Playgroud)

代替

{  
   "animal":{  
      "type":{  
         "key1":"value",
         "key2":"value2"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

可以说key1=value1应该匹配并且response1.json应该返回,或者当key1=时someOtherValueresponse2.json应该返回。是的,钥匙是类型的一部分,而类型是动物的一部分。我怎样才能实现这个请求正文匹配?

Ste*_*ath 4

你可以这样做:

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

另外https://github.com/tomakehurst/wiremock/issues/575

  • 这是另外一个: `"bodyPatterns" : [ { "contains": "type: CREDIT" } ]` (7认同)