I defined query parameter in my contract. I need this parameter to be optional:
method 'GET'
url($(regex(urlRegex))) {
queryParameters {
parameter 'fitler': $(stub(regex(filterRegex)))
}
}
I want this contract to be suitable for the both URLs with filter like /my/sample/url?fitler=some-filter-expression
and without the filter param like /my/sample/url
.
How can I achieve this? Is this even possible?
So far, this has no explicit way defined in WireMock spec. However, you have a workaround using regex, by specifying the URL using urlPathPattern
property (in JSON stubbing). Refer to the example below.
{
"request": {
"method": "GET",
"urlPathPattern": "/myapp/users(\\?((a-zA-Z\\d\\_\\-)+\\=(a-zA-Z\\d\\_\\-)+)(\\&(a-zA-Z\\d\\_\\-)+\\=(a-zA-Z\\d\\_\\-)+)+)?"
},
"response": {
"status": 200,
"bodyFileName": "users.json",
"headers": {
"Content-Type": "application/json"
}
}
}
Observe the optional portion at the end of the URL, which looks for the typical URL query structure. This, I have tried out in wiremock and it runs smoothly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.