alexliesenfeld / httpmock

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

Add `When::query_unset` for a criterion that a query parameter is not present #75

Closed ahl closed 1 year ago

ahl commented 1 year ago

First: love this crate. Thank you.

I have a situation where I want a server with two similar mocks: one where a particular query parameter is set; another where it is not. I can make it work by specifying the query-set mock first, but that ordering feels a little fragile. I would love it if there were a way to do something like this:

    let unset = server.mock(|when, then| {
        when.query_unset("foo");
        // ...
    });
    let set = server.mock(|when, then| {
        when.query_param("foo", "bar");
        // ...
    });

If you'd be amenable, I'd be happy to submit a PR for this functionality.

alexliesenfeld commented 1 year ago

Hi @ahl ! Thanks for creating this issue. Do you think matches would be sufficient for you?

https://github.com/alexliesenfeld/httpmock/blob/d87ab088fff6c5f2e2aacf8083874f357111b1cd/tests/examples/showcase_tests.rs#L29

ahl commented 1 year ago

I see! So something like this:

        when.matches(|req| {
            req.query_params
                .as_ref()
                .and_then(|qs| qs.iter().find(|(key, _)| key == "x"))
                .is_none()
        });

That works; thanks. Feel free to close this

balbifm commented 5 months ago

Hi, wasn't sure if should open another issue, but related to this exact request: Would it still make sense to add such criterion for query_params and headers given that matches does not work for remote mock servers?

Thanks in advance!