如何解决Spring Cloud Contract - 从正则表达式随机生成的空路径参数
我试图通过以下方式在 urlPath
中引入路径参数作为正则表达式:
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
description("""
Represents a success scenario for searching Location Groups.
""")
method 'GET'
urlPath(value(regex('/trackedItems/([a-zA-Z0-9]*)/locationOccupancies'))) {
queryParameters {
parameter 'groupId' : anynonblankString()
}
}
}
response {
status OK()
body(
objects: [
id : $(anyPositiveInt()),locationName : $(anynonblankString()),locationDescription : $(anynonblankString()),locationType : $(anynonblankString()),locationEntry : $(iso8601WithOffset()),locationExit : $(iso8601WithOffset()),trackedItemId : $(fromrequest().path(1)),trackedItemQuantity : $(anyPositiveInt()),]
)
}
}
总的来说,它似乎可以工作,但是有时会在路径中使用空参数生成 java 测试:
// when:
ResponSEOptions response = given().spec(request)
.queryParam("groupId","WMGOAMYTvgoBBZXADCRU")
.get("/trackedItems//locationOccupancies");
同时 wiremock 映射看起来还不错:
{
"id" : "0919f4ee-d487-415e-aaa2-eefbd19833f9","request" : {
"urlPathPattern" : "/trackedItems/([a-zA-Z0-9]*)/locationOccupancies","method" : "GET","queryParameters" : {
"groupId" : {
"matches" : "^\\s*\\S[\\S\\s]*"
}
}
},"response" : {
"status" : 200,"body" : "{\"objects\":{\"id\":527805002,\"locationName\":\"UWHAHTXXYOZNEOKNKGTC\",\"locationDescription\":\"JQZGDLZTXCAUAITPYJLX\",\"locationType\":\"JVUVLFHVBXWHSMACUZUL\",\"locationEntry\":\"0737-06-03T21:45:28+06:24\",\"locationExit\":\"3625-06-03T23:14:29Z\",\"trackedItemId\":\"{{{request.path.[1]}}}\",\"trackedItemQuantity\":1392018943}}","transformers" : [ "response-template","spring-cloud-contract" ]
},"uuid" : "0919f4ee-d487-415e-aaa2-eefbd19833f9"
}
我的合同有问题吗?也许有更好的方法来做到这一点?
id "org.springframework.cloud.contract" version "3.0.1"
id 'org.springframework.boot' version '2.3.1.RELEASE'
谢谢!!
编辑: id "org.springframework.cloud.contract" 版本 "3.0.2" 相同
解决方法
问题出在正则表达式上。应该是 /trackedItems/([a-zA-Z0-9]*)/locationOccupancies
/trackedItems/([a-zA-Z0-9]+)/locationOccupancies
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。