小编Sam*_*Sam的帖子

Wiremock Stand alone - 如何使用请求数据操作响应

我试图使用Wiremock Standalone服务器实现POST REST调用的模拟.我面临这样的挑战,假设帖子正文包含一个"名称"字段及其值,应该在该POST调用的响应中返回相同的值.我的json文件如下所示:

{
"priority": 1,
"request": {
    "method": "POST",
    "urlPath": "/primeSlots",
    "bodyPatterns" : [ {
        "matchesJsonPath" : "{ \"things\": [ { \"name\": \"794363\" }] 
 }"
    } ]
 },
 "response": {
    "status": 200,
    "body": "{{$.things.name.value}}",
    "transformers": ["response-template"]
 }
}
Run Code Online (Sandbox Code Playgroud)

所以,我需要获得值,即794363,但使用上述方法无法在post响应体中获取它.

我也试过这个:

{
 "request": {
    "method": "POST",
    "urlPath": "/transform",
    "bodyPatterns": [
        {
            "matchesJsonPath" : "$.things[?(@.name =~ /[0-9]+/i)]"
        }
    ]
  },
  "response": {
    "status": 200,
    "body": "{\"responseName\": \"
   {{request.body.things.name.value}}\"}",
    "headers": {
        "Content-Type": "application/json"
    },
    "transformers": ["body-transformer"]
  }
 }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,即使我使用的regEx与请求中的任何数字相匹配,如何使用Wiremock独立的json文件在响应中返回相同的数字?谢谢.

java mocking stub wiremock

7
推荐指数
1
解决办法
6727
查看次数

标签 统计

java ×1

mocking ×1

stub ×1

wiremock ×1