alexliesenfeld / httpmock

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

Feature request: multiple expectations in a single server. #51

Closed yellowred closed 2 years ago

yellowred commented 2 years ago

My client needs to fetch user group and it's users from a Keycloak. This requires 3 requests in a single function. I would like to test it by mocking Keycloak using a single server, because my client takes server url in the constructor, so it does not support different host per Keycloak endpoint. Here is my code:

        let server = MockServer::start();
        server.mock(|when, then| {
            when.method(POST)
                .path("/auth/realms/htm/protocol/openid-connect/token")
                .method(GET)
                .path("/auth/admin/realms/htm/groups/uuid1")
                .method(GET)
                .path("/auth/admin/realms/htm/groups/uuid1/members");
            then.status(200)
                .header("content-type", "application/json")
                .body(b_at)
                .status(200)
                .header("content-type", "application/json")
                .body(b_g)
                .status(200)
                .header("content-type", "application/json")
                .body(b_gm);
        });

        let k = keycloak_with_url(server.url(""));
        let res = k.user_grpup(&"uuid1".to_string()).await;

So I would like to define 3 expectations, but they seem to be overwritten. Would it be possible to have a Vec, so one server can serve several endpoints at once? Please correct me if I am doing something wrong or it is already possible (haven't fount in examples).

alexliesenfeld commented 2 years ago

You can actually create multiple mocks. So just call server.mock for every HTTP endpoint you would like to mock.