Wiremock独立-如何处理请求数据的响应 [英] Wiremock Stand alone - How to manipulate response with request data

查看:467
本文介绍了Wiremock独立-如何处理请求数据的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Wiremock独立服务器实现POST REST调用的模拟.我面临这样的挑战,假设发布主体包含一个名称"字段及其值,在该POST调用的响应中应返回相同的值.我的json文件看起来像这样:

I was trying to implement mocking of POST REST call using Wiremock Standalone server. I am faced with a challenge like this, suppose the post body contains an "name" field and its value, the same value should be returned in the response of that POST call. My json file looks like this:

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

因此,我需要获取794363的值,但是使用上述方法无法在响应后的正文中获取它.

so, I need to get the value , that is 794363,but using above approach not able to get it in the post response body.

我也尝试过:

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

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

So my question is, even if i use a regEx, which matches with any number that comes in the request, how to give back the same number in the response using Wiremock standalone json file? Thanks.

推荐答案

不幸的是,WireMock的响应模板转换器当前并未将请求主体分解为Map,而这正是您要执行的操作所必需的.请求正文只是一个字符串.

Unfortunately WireMock's response templating transformer doesn't currently break out the request body into a Map as would be necessary for what you're trying to do. The request body is just a single string.

启用此功能最简单的方法可能是编写一个实现JSONPath或其他机制来查询JSON文档的Handlebars帮助器,然后在初始化WireMock时将其注册到模板转换器中.

The simplest way for you to enable this would probably be to write a Handlebars helper that implements JSONPath or some other mechanism for querying a JSON document, then registering this with the templating transformer when you initialise WireMock.

有时,我会编写把手助手来针对XML和JSON进行此类操作,但这不会持续一段时间.

At some point I'll write handlebars helpers to do this kind of thing for XML and JSON, but that won't be for a while.

这篇关于Wiremock独立-如何处理请求数据的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆