LukeMathWalker / wiremock-rs

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

Report diff between expected and actual request body sent to a mock #95

Open mdenburger opened 2 years ago

mdenburger commented 2 years ago

It would be nice to be able to fail a test with a clear diff between the expected and actual request body sent to a mock.

Use case: I'm mocking a third-party XML-based API and want to verify that all requests to it contain the expected data. My mocks currently have a MethodExactMatcher, PathExactMatcher and BodyExactMatcher so they only respond when the exact expected API call is done. For example:

Mock::given(method(Post))
        .and(path(request_path))
        .and(body_string(expected_request_body))
        .respond_with(response_template)
        .mount(mock_server)
        .await;

The drawback of this approach is that when the actual request body contains a small error - the most common place for mistakes - a test fails with a 404 Not Found response because the BodyExactMatcher returns false. Those failures are a bit cumbersome to debug, having to enable debug logging so my code logs sent messages and rerun the test. It would be much nicer when a test failed with a clear diff between the expected and actual request body.

Is there a way to create such behavior with the current wiremock APIs? As far as I can see a custom matcher cannot report errors, and there's no way to add custom expectations either.

LukeMathWalker commented 2 years ago

I don't believe this is possible given the current API, but it'd definitely be a significant improvement in ergonomics. I'll think about a mechanism to make that feasible 🤔