7

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?

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
yaroslavTir
  • 711
  • 2
  • 10
  • 22
  • 2
    Have you managed to achieve this? I'm dealing with the same problem right now – tsayen Dec 11 '17 at 16:08
  • 1
    I have the same problem. What I found in my researches was that just create multiple contracts for presence or absence of that query param :| – hossein shemshadi Jul 21 '18 at 06:54
  • 2
    spring cloud contract uses wiremock behind the scene, and it does not seem wiremock supports optional query parameter: https://groups.google.com/forum/#!topic/wiremock-user/WKMkb_LhJTU – ThanksForAllTheFish Nov 14 '18 at 15:09

1 Answers1

1

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.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35