wiremock-rs icon indicating copy to clipboard operation
wiremock-rs copied to clipboard

Fail tests on unexpected call

Open sazzer opened this issue 2 years ago • 2 comments

Right now, if a MockServer receives an unexpected call then this gets logged and an HTTP 404 is returned to the client.

It would be useful if there was some way to have the server instead fail tests if it receives an unexpected request. That would not only be more obvious in outputs - especially if the client is expecting a 404 from the server and gets it for the wrong reason - but it also means that, if using WireMock to emulate a real remote service, unexpected API calls to it will be caught quickly.

Cheers

sazzer avatar May 16 '22 06:05 sazzer

I can see at least four ways this could be achieved:

  1. Support for a handler that can handle any request but at a lower priority, so that it comes after all others
  2. Some way to indicate in MountedMockSet that unexpected calls should panic instead of returning a 404
  3. Some (sync) way to return at least the count of calls that hit the auto-404 handler in MountedMockSet
  4. Actually have MountedMockSet.verify_all() panic if it ever encountered any unexpected calls

I've no idea which of those would be preferable though.

sazzer avatar May 16 '22 07:05 sazzer

I've been using approach 1:

        Mock::given(any())
            .respond_with(|req: &_| panic!("Request did not match any expected requests: {req:#?}"))
            .with_priority(u8::MAX)
            .mount(&server)
            .await;

The produced error isn't great, but you can make do.

sunjay avatar Apr 25 '24 19:04 sunjay