Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use wiremock to intercept a POST request in order to check it's query params

Is it possible to have a test using Wiremock for a known POST request, in order to check the query params the request has?

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();

I don't know if I am on the right path, I can't find good examples in the documentation.

like image 536
Alex Avatar asked Sep 17 '25 08:09

Alex


1 Answers

All of those parameters and headers that you added will have to be present in the request in order to be matched by WireMock. That should test the presence of query parameters in request properly. If you have issues matching the request with the stub, you can stop the code execution with breakpoint after your stub is registered and evaluate (ALT + F8 in IntelliJ) this expression:

    WireMock.listAllStubMappings()

I found some repositories with examples containing full configuration in the GitHub of WireMock author: https://github.com/tomakehurst/wiremock-demo and https://github.com/tomakehurst/wiremock-presentation-examples

I'm not sure if that answers your question entirely. If you have any specific issues, then please provide the code that causes it: https://stackoverflow.com/help/minimal-reproducible-example

like image 99
mklepa Avatar answered Sep 19 '25 23:09

mklepa