Spring Cloud Contract - 从正则表达式中随机生成的空路径参数
Spring Cloud Contract - empty path parameter randomly generated from the regexp
我试图通过以下方式将 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" version "3.0.2"
相同
问题出在正则表达式上。而不是 /trackedItems/([a-zA-Z0-9]*)/locationOccupancies
应该是 /trackedItems/([a-zA-Z0-9]+)/locationOccupancies
我试图通过以下方式将 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" version "3.0.2"
相同问题出在正则表达式上。而不是 /trackedItems/([a-zA-Z0-9]*)/locationOccupancies
应该是 /trackedItems/([a-zA-Z0-9]+)/locationOccupancies