alexliesenfeld / httpmock

HTTP mocking library for Rust
https://httpmock.rs
MIT License
488 stars 45 forks source link

Add `Then::remove_after_n` to self-delete a mock after N hits #76

Closed ahl closed 1 year ago

ahl commented 1 year ago

Again: love this crate.

I've found some situations where I would like a Then to specify a response (in my case once) and then for a subsequent call with the same parameters to get a different response. One way I could imagine doing it is like this:

    let mock1 = server.mock(|when, then| {
        when.any_request();
        then.status(200).remove_after_n(1);
    });
    let mock2 = server.mock(|when, then| {
        when.any_request();
        then.status(503);
    });

Happy to submit a PR if this seems reasonable. Thanks.

alexliesenfeld commented 1 year ago

Hi @ahl ! If a manual removal is sufficient for you, you could use mock1.delete(), like here: https://github.com/alexliesenfeld/httpmock/blob/d87ab088fff6c5f2e2aacf8083874f357111b1cd/tests/examples/delete_mock_tests.rs#L27

ahl commented 1 year ago

I noticed Mock::delete but the situation we're considering basically looks like this:

let mock1 = server.mock(...); // respond with transient failure
let mock2 = server.mock(...); // respond with success

operation_that_should_retry_failed_queries();

So we don't (I think?) have an opportunity to call delete.

ahl commented 1 year ago

@alexliesenfeld any more thoughts on this? Thanks.

alexliesenfeld commented 1 year ago

Sure, I would accept a PR! However, I think for your specific use case it could be an option to use Mock::assert_hits (see docs here).

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

ahl commented 1 year ago

I'll work on a PR; assert_hits doesn't do what we have in mind here.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.