Hello! I'm not sure how WireMock behaves with quer...
# help
s
Hello! I'm not sure how WireMock behaves with query parameters and I couldn't find any info about it. What I want to know is if I need to create two stubs when my endpoint has optional parameters... do I need to create one stub with the parameter and another one without it?
b
Does the fact that the query parameter is or isn’t there impact the response for your situation? If so, I’d create two responses to keep things simple. If not, you can just ignore the query parameter.
s
how do I ignore it? If I don't define a query paremeter will the stub match if the method and path match?
b
If you use
urlPathEqualTo()
or
urlPathMatching()
query parameters will be ignored. See https://wiremock.org/docs/request-matching/#url-matching
s
I'm using
urlPathTemplate
because I need to extract some info with regular expressions... would query parameters be ignored in that case? Sorry I have so many questions, just starting with WireMock and I have not been able to find this info
b
No worries, we’re here to help.
urlPathMatching()
takes a regex if I recall correctly. Are you defining responses in code or in JSON files?
s
JSON, I'm defining a ConfigMap with my stubs in JSON and WireMock is picking that up
b
s
ok, so what about using a patter to match the path but also matching query parameters... is that not possible?
b
Sure, you can combine multiple matchers easily
s
ok so in this case
Copy code
"request": {
    "method": "GET",
    "urlPathTemplate": "/v1/profiles/{profileId}/groups",
    "pathParameters": {
      "profileId": {
        "matches": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
      }
    },
    "queryParameters": {
      "space": {
        "matches": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
      }
    }
  }
The stub would match a GET to any url with a path that matches that regular expression with a parameter (space) which must be there... am I right?
b
I think so, yes, but it’s hard to know for sure without testing
👍 1
516 Views