1

How do I verify two post requests to the same url but with different bodies in Wiremock? The same url should be called once with body 1 and once with body 2.

As it is now Wiremock only cares about verifying the last line.

verify(postRequestedFor(urlEqualTo("/my-url"))
    .withRequestBody(equalToJson(resourceAsString("my-first-body.json"), true, false)));

verify(postRequestedFor(urlEqualTo("/my-url"))
    .withRequestBody(equalToJson(resourceAsString("my-other-body.json"), true, false)));
Swoot
  • 1,304
  • 11
  • 12
  • https://stackoverflow.com/questions/42661054/wiremock-multiple-responses-for-the-same-url-and-content duplicate. – Dan Rayson Feb 08 '23 at 20:13
  • This is not a duplicate. https://stackoverflow.com/questions/42661054/wiremock-multiple-responses-for-the-same-url-and-content is about generating different responses for the same request, this question is about verifying different request bodies sent to the same URL. – Harald Albers Feb 15 '23 at 09:20

1 Answers1

2

I ended up doing this:

var postRequests = findAll(postRequestedFor(urlMatching("/my-url")));

assertThat(postRequests.get(0).getBodyAsString()).isEqualTo(resourceAsString("my-first-body.json"));
assertThat(postRequests.get(1).getBodyAsString()).isEqualTo(resourceAsString("my-other-body.json"));
Swoot
  • 1,304
  • 11
  • 12
  • If you wanted to make this order indifferent, could you modify your `postRequests` to be two separate variables that have the `equalToJson` filtering, and then verify that they are a certain size? – agoff Apr 30 '21 at 15:18
  • I guess you could use the anyMatch method for both of the files on the postRequests list. – Swoot May 04 '21 at 06:14