【问题标题】:Wiremock standalone dynamic response array of objects is not working with bodyPatterns and matchesJsonPathWiremock 独立的对象动态响应数组不适用于 bodyPatterns 和 matchesJsonPath
【发布时间】:2020-01-28 07:26:11
【问题描述】:

我正在使用wiremock 来存根请求。我创建了一个 json 文件来获得响应:

{
    "request": {
        "method": "POST",
        "urlPath": "/nested/transform",
        "bodyPatterns": [
            {
                "matchesJsonPath": "$.name.[0].first"
            },
            {
                "matchesJsonPath": "$.name.[1].first"
            }
        ]
    },
    "response": {
        "status": 200,
        "body": "{\"firstName\": \"$(name.[0].first)\", \"lastName\": \"$(name.[1].first)\"}",
        "headers": {
            "Content-Type": "application/json"
        },
        "transformers": ["body-transformer"]
    }
}

我的请求和响应如下:

请求

{
"name": [
      {
        "first": "Vijay"
      },
      {
        "first": "Sagar"
      }
   ]
}

在这里我收到了非常有胡子的响应,它没有按我想要的方式解析。

回应这不是我的预期结果:

{
  "firstName": "[{first=Vijay}, {first=Sagar}]",
  "lastName": "[{first=Vijay}, {first=Sagar}]"
}

预期结果是:我愿意根据上述请求和存根 json 收到以下响应:

{"firstName": "Vijay","lastName": "Sagar"}

我尝试了很多但无法匹配响应参数,如何获得预期的结果?

【问题讨论】:

    标签: wiremock wiremock-standalone


    【解决方案1】:

    在处理 JSON 响应时,我更喜欢使用 bodyFileName,因为它不需要转义。

    __files/nested_json_template.json

    {
        "firstName": "{{jsonPath request.body '$.name.[0].first'}}",
        "lastName": "{{jsonPath request.body '$.name.[1].first'}}"
    }
    

    ma​​ppings/nested_json_mapping.json

    {
        "request": {
            "method": "POST",
            "urlPath": "/nested/transform",
            "bodyPatterns": [
                {
                    "matchesJsonPath": "$.name.[0].first"
                },
                {
                    "matchesJsonPath": "$.name.[1].first"
                }
            ]
        },
        "response": {
            "status": 200,
            "bodyFileName": "nested_json.json",
            "headers": {
                "Content-Type": "application/json"
            },
            "transformers": ["response-template"]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多