Reputation: 721
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?
Upvotes: 7
Views: 2854
Reputation: 1756
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.
Upvotes: 1