使用Wiremock拦截POST请求,以检查其查询参数

时间:2019-11-15 12:12:50

标签: java unit-testing wiremock

是否可以使用Wiremock对已知的POST请求进行测试,以便检查请求具有的查询参数?

 wireMockServer.start();
        stubFor(post(urlMatching("http://localhost:8080/smth.*"))
                .withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
                .withQueryParam("authorizationcode", equalTo("123456"))
                .withQueryParam("baseamount", equalTo("0.10"))
                .withQueryParam("basecurrency", equalTo("978"))
                .withQueryParam("cardcountry", equalTo("ITA"))
                .withQueryParam("cardexpirydate", equalTo("0120"))
                .withQueryParam("customfield",equalTo("some+custom+field"))
                .withQueryParam("result", equalTo("APPROVED"))
                .willReturn(aResponse().withBody(RESPONSE))
        );
        wireMockServer.stop(); 

我不知道自己走的路是否正确,在文档中找不到很好的例子。

1 个答案:

答案 0 :(得分:1)

您添加的所有这些参数和标头都必须存在于请求中,以便由WireMock进行匹配。那应该正确测试请求中查询参数的存在。如果在将请求与存根匹配时遇到问题,则可以在存根注册后通过断点停止代码执行并评估(IntelliJ中的ALT + F8)此表达式:

    WireMock.listAllStubMappings()

我在WireMock作者的GitHub中找到了一些包含完整配置示例的存储库: https://github.com/tomakehurst/wiremock-demohttps://github.com/tomakehurst/wiremock-presentation-examples

我不确定这是否能完全回答您的问题。如果您有任何特定问题,请提供导致它的代码: https://stackoverflow.com/help/minimal-reproducible-example