使用 WireMock 时查询参数未匹配

Query Parameter is not getting matched when using WireMock

我正在尝试使用以下存根访问 WireMock,但似乎查询参数未匹配。这是回复:

                                               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
                                                           |

这是我使用的存根:

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

我自己也遇到过同样的问题,但我能够通过使用 queryParameters 重写匹配器来克服。

也许你可以做类似的事情并重写你的模式如下:

{
    "request": {
        "method": "GET",
        "urlPathPattern": "/mpp-pricing/v1/agreements",
        "queryParameters": {
           "accountId": {
              "matches": ".*"
           }
        }
    }
},

您可以re-write url 并使用过滤器查询参数,例如 class 文档中的 UrlAndHeadersModifyingFilter 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));