LukeMathWalker / wiremock-rs

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

Doing something after a `Mock` responded #85

Open johannescpk opened 2 years ago

johannescpk commented 2 years ago

Sometimes it'd be useful to have some logic run after a mock finished responding (so the client doing the request received the response). Currently this doesn't seem to be possible. I've tried to work on it but was blocked by https://github.com/hyperium/hyper/discussions/2731. So any suggestions how this could be handled differently would be appreciated.

LukeMathWalker commented 2 years ago

What kind of logic are you trying to run?

johannescpk commented 2 years ago

In this case the same as https://github.com/LukeMathWalker/wiremock-rs/issues/84#issuecomment-1008873435, tho to make the test work correctly it'd require to run after the mock caller got the response.

onelson commented 6 months ago

I ran into this recently and added a loop to poll the received requests from the mock server in my tests. Something like:

    let mut interval = tokio::time::interval(std::time::Duration::from_millis(10));
    let mut retries = 50;
    loop {
        interval.tick().await;
        retries -= 1;
        let reqs = mock_server.received_requests().await.unwrap_or_default();
        if !reqs.is_empty() || retries <= 0 {
            break;
        }
    }
    mock_server.verify().await;

It would be nicer (for me specifically) if there were some sort of a stream or channel interface where I could either block while waiting on the received request in a cancel-safe way, or poll the next one (returning immediately if there are no new requests buffered in the stream).

For context, in my case the request being made to the mock is a downstream side-effect of things initiated in the test. There's a separate worker thread that sends the request indirectly. This means my test can't just wait for the response to know the whole flow completed; I have to spy on the mock server.