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

Doing something after a `Mock` responded

Open johannescpk opened this issue 3 years ago • 3 comments

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.

johannescpk avatar Jan 07 '22 12:01 johannescpk

What kind of logic are you trying to run?

LukeMathWalker avatar Jan 07 '22 12:01 LukeMathWalker

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.

johannescpk avatar Jan 10 '22 13:01 johannescpk

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.

onelson avatar Feb 25 '24 06:02 onelson