Wiremock - “URL不匹配”,即使它是相同的

2022-09-02 20:26:43

我遇到了一个问题,Wiremock说我的URL不匹配,即使它们是相同的。显然我错过了一些东西。我做错了什么?

WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo("/test/url?bookingCode=XYZ123&lastName=TEST"))
    .willReturn(WireMock.aResponse()
    .withStatus(200))
)

下面是控制台日志。

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/test/url?bookingCode=XYZ123&lastName=TEST                 | /test/url?bookingCode=XYZ123&lastName=TEST            <<<<< URL does not match
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

是因为我没有在匹配器中包含标题吗?

如果是,如何避免匹配标头?无论我发送什么标头,我都希望得到响应。


答案 1

找到了原因.. 不应具有查询参数。WireMock.urlPathEqualTo("/test/url?bookingCode=XYZ123&lastName=TEST")

更改它以解决问题。WireMock.urlPathEqualTo("/test/url")

文档说这是允许的。此外,日志导致混淆。考虑到匹配检查是单独的,wiremock 可以为查询参数添加单独的日志。URL does not match

已创建问题 : https://github.com/tomakehurst/wiremock/issues/1262


答案 2

您可以使用参数的方法,同时保持方法专用于URL路径。withQueryParamurlPathEqualTo

WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo("/test/url"))
         .withQueryParam("bookingCode", WireMock.equalTo("XYZ123"))
         .withQueryParam("lastName", WireMock.equalTo("TEST"))
         .willReturn(WireMock.aResponse()
         .withStatus(200))

欲了解更多信息,请参阅 http://wiremock.org/docs/request-matching/


推荐