alexliesenfeld / httpmock

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

RFC: simple statefull when-clauses #73

Closed empwilli closed 1 year ago

empwilli commented 1 year ago

Scenario In my test scenario, my client performs multiple similar requests and I need the mock server to respond differently. (In my scenario I want to emulate concurrent resource updates that my function under test should tolerate).

Problem The requests are not distinguishable, i.e. I can't create two different when() clauses for the two (or more) requests.

Solution The solution would be to make the mock server somewhat "statefull". The matches() method of When already captures custom function pointers to match the request body against. One possible solution that comes to mind would be to have the server optionally store a reference to a State trait that the test can implement. One could imagine a new clauses for when() and then() (let's name them check_state and update_state for the lack of a better name):

let first_request = server.mock(|when, then| {
    when.method(GET)
        .check_state(|state: impl State| {
// resource available
    };
    then.status(200););
});

let first_request = server.mock(|when, then| {
    when.method(DELETE);
    then.status(200)
           .update_state(|state: impl State| {
// update state
    };);
});

let second_request = server.mock(|when, then| {
    when.method(GET)
        .check_state(|state: impl State| {
// resource not available
    };
    then.status(404););
});
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.