Query Parameter is not getting matched when using WireMock
Asked Answered
M

3

5

I am trying to hit the WireMock with following stub but it seems that the query param is not getting matched. Here is the response:

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/mpp-pricing/v1/agreements\?accountId=.*                   | /mpp-pricing/v1/agreements?accountId=5388afaf-ee3d-44ed-a<<<<< URL does not match. When using a regex, "?" should be "\\?"
                                                           | b2a-0035156bb0a2
                                                           |

and this is the stub I used:

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/mpp-pricing/v1/agreements\\?accountId=.*"
  },
Mala answered 4/12, 2019 at 12:34 Comment(0)
A
9

I have faced the same issue myself but I was able to overcome by re-writing the matcher using queryParameters.

Maybe you could do something similar and re-write your pattern as below:

{
    "request": {
        "method": "GET",
        "urlPathPattern": "/mpp-pricing/v1/agreements",
        "queryParameters": {
           "accountId": {
              "matches": ".*"
           }
        }
    }
},
Acridine answered 17/12, 2019 at 16:41 Comment(0)
M
1

It is essential to understand the differences between: urlPatternand urlPathPattern. I hope this post helps.

If you choose urlPathPattern you can't use a URL with query params.

In this way, the stub that should work is:

  "request": {
    "method": "GET",
    "urlPattern": "/mpp-pricing/v1/agreements\\?accountId=.*"
  },

It's useless the urlPathPattern here because the regex would only match on the path. In this case, the regex is in the query param so you need to add a queryParameters object with a matches expression as @Dio answered earlier.

So this is the result using urlPath and queryParameters:

{
    "request": {
        "method": "GET",
        "urlPath": "/mpp-pricing/v1/agreements",
        "queryParameters": {
           "accountId": {
              "matches": ".*"
           }
        }
    }
},
Mesmerize answered 7/12, 2022 at 14:46 Comment(0)
L
0

You can re-write the url and query parameters by using filters, an example class UrlAndHeadersModifyingFilter in documentation https://wiremock.org/docs/extending-wiremock

public static class UrlAndHeadersModifyingFilter extends StubRequestFilter {

    @Override
    public RequestFilterAction filter(Request request) {
        Request wrappedRequest = RequestWrapper.create()
                .transformAbsoluteUrl(url -> url + "?extraQueryParam=123")
                .addHeader("X-Custom-Header", "headerval")
                .wrap(request);

        return RequestFilterAction.continueWith(wrappedRequest);
    }

    @Override
    public String getName() {
        return "url-and-header-modifier";
    }
} 

new WireMockServer(wireMockConfig()
  .extensions(UrlAndHeadersModifyingFilter.class));
Lastditch answered 7/4, 2022 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.