alexliesenfeld / httpmock

HTTP mocking library for Rust.
MIT License
435 stars 40 forks source link

Mocking retries? #96

Open jamesperez2005 opened 5 months ago

jamesperez2005 commented 5 months ago

I'm trying to mock a service that is supposed to be called multiple times and fail, and then succeed at the Nth time. Since these are retries the requests should be the same, so there's no way to distinguish them by parameters or by path/body.

Is that something that can be done with httpmock?

alexliesenfeld commented 5 months ago

Hi @jamesperez2005 , thanks for reaching out!

Do you think delete will help you here?


#[test]
fn delete_mock_test() {
    let server = MockServer::start();

    let mut m1 = server.mock(|when, then| {
        when.method(GET).path("/test");
        then.status(500);
    });

    let mut m2 = server.mock(|when, then| {
        when.method(GET).path("/test");
        then.status(200);
    });

    let response = get(&format!(
        "http://{}:{}/test",
        server.host(),
        server.port()
    ))
    .unwrap();

    assert_eq!(response.status(), 500);

    // As long as both m1 and m2 exist, m1 will take precedence over m2. 
    // Let's delete m1 so we can respond with a successful response.
    m1.delete();

    let response = get(&format!("http://{}/test", server.address())).unwrap();

    assert_eq!(response.status(), 200);
}
jamesperez2005 commented 5 months ago

Not quite. I want to test a utility function that does the retries by itself, so I don't get a chance to intervene after the first N retries... It would be great to have a "match_at_most(N)" helper on the when clause, or even provide a callback to control what happens based on a variable

alexliesenfeld commented 2 months ago

Interesting idea ... OK, let's keep this issue open and I'll try to get a PR for it after the next release (it's a big change, so it makes sense to wait a bit).