LukeMathWalker / wiremock-rs

HTTP mocking to test Rust applications.
Apache License 2.0
617 stars 70 forks source link

Fail tests on unexpected call #93

Open sazzer opened 2 years ago

sazzer commented 2 years ago

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 commented 2 years ago

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.

sunjay commented 4 months ago

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.