Skip to content

Mocking retries? #96

Closed
Closed

Description

@jamesperez2005

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?

Activity

  1. alexliesenfeld commented on Jan 7, 2024

    @alexliesenfeld
    Member

    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);
    }
  2. jamesperez2005 commented on Jan 8, 2024

    @jamesperez2005
    Author

    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

  3. alexliesenfeld commented on May 3, 2024

    @alexliesenfeld
    Member

    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).

  4. added
    enhancement
    on Jun 2, 2024
  5. mothran commented on Oct 31, 2024

    @mothran

    Until #108 lands here is a (ugly) work around to mock failing the first request:

    static mut REQ_COUNT: u32 = 0;
    ...
    let server = MockServer::start();
    let get_mock = server.mock(|when, then| {
        when.method(GET).path("/test").matches(|_req: &HttpMockRequest| {
            let req = unsafe {
                let req = REQ_COUNT;
                REQ_COUNT += 1;
                req
            };
            req >= 1
        });
        then.status(200).body("TEST");
    });

    Its not pretty but is a reasonable workaround.

  6. vtereso commented on Apr 16, 2025

    @vtereso

    +1 on this as a critical feature.

  7. rldleblanc commented on Dec 4, 2025

    @rldleblanc

    I have this same exact need. With <0.8 I was able to use global variables and atomics to track the count in the .match(), but as of 0.8, I can no longer do that with .is_true() since all mocks are checked causing issues with the counting. Since I need to send back a 5XX error with a payload I have to run with two mocks and that's double counting the global variable in my case. Would love to see traction on this feature.

  8. FalkWoldmann commented on Oct 20, 2026

    @FalkWoldmann
    Member

    Implemented in #108 with up_to_n_times, will be in 0.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions