线模发布请求 json body 属性值是动态生成的值如何存根 [英] wiremock post request json body property value is dynamic generated value how to stub

查看:61
本文介绍了线模发布请求 json body 属性值是动态生成的值如何存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 wiremock 的新手,我在 Spring Boot 集成测试中使用了 wiremock 来模拟外部 http 调用之一.

我的 json 请求正文包含两个动态属性,所有其他字段都可以在请求中设置和存根,只有两个字段值无法设置,因为它们是动态值.

由于这些本质上是动态的,我的意思是属性的值是随机自动生成的值.有没有办法忽略这些属性,而剩下的所有属性无论如何都是匹配的.

我知道可以忽略额外的字段和数组顺序

withRequestBody(equalToJson(requestJson, true, true))

<块引用>

true ignoreArrayOrder

true ignoreExtraField

我想知道类似于上面的,忽略特定属性的功能(用户可以提及属性名称,以便它应该忽略的属性)是否可用?

我有一个以下 json 请求正文,我需要在模拟帖子中存根,所以我正在使用

requestJson

<代码>{"property_one": "阿南德糖果","property_two": "Guru Sweets","property_three": "Kranthi Sweets","property_four" : auto-generated-number,//来自代码的动态数字property_five":空,"propertysix" : " "GST128093","property_seven" : auto-generated-number,//来自代码的动态数字property_8":13890139,property_nine":1290.90,property_ten":X239GDIJD9090"

}

存根

stubFor(post(urlEqualTo(testUrl)).withHeader(CUSTKEY, equalTo(CUST_VALUE)).withHeader(TOKENKEY, equalTo(TOKEN_VALUE)).withHeader(MATCHKEY, equalTo(MATCH_VALUE)).withRequestBody(equalToJson(requestJson, true, true)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(responseJson)));

responseJson

<代码>{成功":真实,"message": "请求处理成功",成功代码":9000,外部":AKALDJKAD138948934"}

在请求 json 中,我有大约 60 到 70 个属性,除了 property_four 和 property_seven 其余所有字段匹配都正确发生.

我想知道如何在 wiremock 中使用 equaltoJson(requestJson) 忽略这两个字段,因为这些字段值本质上是动态的,我想从匹配中跳过这些属性.

任何人都知道如何跳过或忽略这些属性或任何其他解决此问题的好方法.

我想过使用 withRequestBody(matchingJsonPath(PATH)),但使用 巨大而复杂的请求

我觉得这不是正确的解决方案.

我的请求正文太复杂了,我的意思是很多字段,只是举个例子,我只提到了十个带有示例属性和值的属性.

提前致谢,正如我之前所说,我是wiremock 的新手.

解决方案

假设您使用的是 WireMock 版本 2.26.3,您可以对请求中的任何 JSON 值使用占位符.请参阅请求匹配文档了解更多信息(可能最好搜索占位符").

<代码>{"property_one": "阿南德糖果","property_two": "Guru Sweets","property_three": "Kranthi Sweets","property_four" : "${json-unit.any-number}",//来自代码的动态数字property_five":空,"propertysix" : " "GST128093","property_seven" : "${json-unit.any-number}",//来自代码的动态数字property_8":13890139,property_nine":1290.90,property_ten":X239GDIJD9090"}

对于 2.26.3 之前的 WireMock 版本,JSON 匹配不太可靠,我绝对建议升级.

Am new to wiremock, am using wiremock in my spring boot integration test for mocking one of the external http call.

My json request body contains two dynamic properties, all other fields am able to set and stub in the request only two field values am not able to set as they are dynamic values.

As these are dynamic in nature, I mean value of the property is random-auto-genarated values. Is there a way to ignore these properties and remaining all properties are anyhow matching.

I know that ignore is available for extra fields and array order

withRequestBody(equalToJson(requestJson, true, true))

true ignoreArrayOrder

true ignoreExtraField

I want to know similar to above, ignore feature to specific property (user can mention the property name so that property it should ignore) is available?

I have a following json request body that I need to stub in wire mock post so am using

requestJson

{
   "property_one" : "Anand Sweets",
   "property_two" : "Guru Sweets",
   "property_three" : "Kranthi Sweets",
   "property_four" : auto-generated-number,  // dynamic number from code
   "property_five" : null,
   "propertysix" : " "GST128093",
   "property_seven" : auto-generated-number, // dynamic number from code
   "property_eight" : 13890139,
   "property_nine" : 1290.90,
   "property_ten" : "X239GDIJD9090"

}

Stubbing

stubFor(post(urlEqualTo(testUrl))
            .withHeader(CUSTKEY, equalTo(CUST_VALUE))
            .withHeader(TOKENKEY, equalTo(TOKEN_VALUE))
            .withHeader(MATCHKEY, equalTo(MATCH_VALUE))
            .withRequestBody(equalToJson(requestJson, true, true))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/json")
                    .withBody(responseJson)
            )
    );

responseJson

{
     "Success": true,
     "message": "request processed successfully",
     "success_code": 9000,
     "external": "AKALDJKAD138948934" 
}

Here in the request json, I have around almost 60 to 70 properties, except for property_four and property_seven remaining all fields match is happening properly.

I wanted to know how to ignore these two fields with equaltoJson(requestJson) in wiremock, as these field values are dynamic in nature, I wanted to skip these properties from matching.

Any one have any Idea how to skip or ignore these properties Or any other good solution to this problem.

I thought of using withRequestBody(matchingJsonPath(PATH)) but with huge and complex request,

I felt, that is not the right solution.

My request body is so complex, I mean many fields, just for example, I mentioned only ten properties with example properties and values.

Thanks in advance, as I said earlier am new to wiremock.

解决方案

Assuming you are using WireMock version 2.26.3, you can use placeholders for any JSON value in the request. See the request matching documentation for more information (probably best to search for "placeholders").

{
    "property_one" : "Anand Sweets",
    "property_two" : "Guru Sweets",
    "property_three" : "Kranthi Sweets",
    "property_four" : "${json-unit.any-number}",  // dynamic number from code
    "property_five" : null,
    "propertysix" : " "GST128093",
    "property_seven" : "${json-unit.any-number}", // dynamic number from code
    "property_eight" : 13890139,
    "property_nine" : 1290.90,
    "property_ten" : "X239GDIJD9090"
}

For versions of WireMock prior to 2.26.3, the JSON matching isn't quite as robust, and I'd definitely just recommend upgrading.

这篇关于线模发布请求 json body 属性值是动态生成的值如何存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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