使用 wiremock 拦截 POST 请求以检查它的查询参数
Use wiremock to intercept a POST request in order to check it's query params
是否可以使用 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();
我不知道我走的路对不对,我在文档中找不到很好的例子。
所有这些参数和您添加的 headers 都必须出现在请求中才能被 WireMock 匹配。那应该正确地测试请求中查询参数的存在。如果您在将请求与存根匹配时遇到问题,则可以在注册存根后使用断点停止代码执行并评估(IntelliJ 中的 ALT + F8)此表达式:
WireMock.listAllStubMappings()
我在 WireMock 作者的 GitHub 中找到了一些包含完整配置示例的存储库:
https://github.com/tomakehurst/wiremock-demo 和
https://github.com/tomakehurst/wiremock-presentation-examples
我不确定这是否完全回答了您的问题。如果您有任何具体问题,请提供导致问题的代码:
https://whosebug.com/help/minimal-reproducible-example
是否可以使用 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();
我不知道我走的路对不对,我在文档中找不到很好的例子。
所有这些参数和您添加的 headers 都必须出现在请求中才能被 WireMock 匹配。那应该正确地测试请求中查询参数的存在。如果您在将请求与存根匹配时遇到问题,则可以在注册存根后使用断点停止代码执行并评估(IntelliJ 中的 ALT + F8)此表达式:
WireMock.listAllStubMappings()
我在 WireMock 作者的 GitHub 中找到了一些包含完整配置示例的存储库: https://github.com/tomakehurst/wiremock-demo 和 https://github.com/tomakehurst/wiremock-presentation-examples
我不确定这是否完全回答了您的问题。如果您有任何具体问题,请提供导致问题的代码: https://whosebug.com/help/minimal-reproducible-example