LukeMathWalker/wiremock-rs

Store received requests

tl-alex-butler opened this issue · 1 comments

Currently, as far as I can see, all verification is done implicitly by matchers.

However, when testing against mock APIs I tend to prefer setting up very simple mock server matcher logic with manually verifying the received requests afterwards.

This approach can be nicer in some cases as:

  • It avoids the mock-server 404ing a subtly different request.
  • It makes fine grained verification of request fields much easier.
  • It makes test failure messages much more accurate when the request verification fails, rather than some 500 caused by a wayward 404 from the mock server.

Proposal

I'd like to add some sort of API to retrieve received requests from a mock server.

Then scenarios could go something like:

Mock::given(method("GET"))
  .and(path("/foo"))
  .respond_with(ResponseTemplate::new(200).set_body_json(...))
  // ...

run_scenario();

let received_requests = // get the received requests to the mock server `GET /foo` somehow
verify_scenario_requests(received_requests);

Other wiremock-ish libs have this sort of thing, e.g. c# one has .FindLogEntries(matcher). We could make all mock-server automatically record requests, or perhaps make it opt in only.

Sounds good to me - having some form of recording functionality was part of the plan anyway to support a "record & replay"/discovery mode. It could also improve error messages massively (e.g. "XYZ was not verified. Here is the list of requests received by the mock server").
I'll spec it out a bit and then come up with a PR!