我正在使用wiremock在Spring启动应用程序中为Junit模拟休息服务。我的问题是,我无法匹配多种匹配模式。
Junit.java
StringValuePattern pattern = WireMock.matching(".*");
givenThat(post(urlEqualTo("/softwares"))
.withHeader("Content-Type", equalTo("application/json"))
//TODO: Matching exact JSON body
.withRequestBody(pattern)
.willReturn(aResponse()
.withStatus(201)
.withHeader("Content-Type", "application/json")
.withBody("{\"userId\":\"ID009\"}")));
以上代码片段,我使用(。*)进行匹配。它工作正常。但是,我需要匹配在wiremock的映射文件夹
中部署的JSON中的确切内容"matchesJsonPath" : "$.name",
"matchesJsonPath" : "$.protocol",
"matchesJsonPath" : "$.website",
"matchesJsonPath" : "$.website.user",
"matchesJsonPath" : "$.website.urlpath",
"matchesJsonPath" : "$.website.userlist",
"matchesJsonPath" : "$.website.resources.friendsList"
示例Json - 部署在wiremock的映射文件夹
中
{
"request" : {
"urlPath" : "/softwares",
"method" : "POST",
"basicAuthCredentials" : {"username" : "username", "password" : "password"},
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
},
"bodyPatterns" : [ {
"matchesJsonPath" : "$.name",
"matchesJsonPath" : "$.protocol",
"matchesJsonPath" : "$.website",
"matchesJsonPath" : "$.website.user",
"matchesJsonPath" : "$.website.urlpath",
"matchesJsonPath" : "$.website.userlist",
"matchesJsonPath" : "$.website.resources.friendsList"
} ]
},
"response" : {
"status" : 201,
"headers" : { "Content-Type" : "application/json"},
"body": "{ \"userId\" : \"ID009\"}"
}
}
答案 0 :(得分:0)
您应该尝试extending wiremock。
每当请求到来时,这会将请求转发给方法,在该方法中,您可以获取请求字符串,然后执行所有验证并返回/重新路由/拒绝请求。这是一个非常强大的功能。