Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing the request body in Then #103

Open
nastynaz opened this issue Apr 12, 2024 · 3 comments
Open

Accessing the request body in Then #103

nastynaz opened this issue Apr 12, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@nastynaz
Copy link

nastynaz commented Apr 12, 2024

Is it possible to access the passed-in request body in Then? I'd like for the mock server to return a response based on the input. e.g:

let mock = server.mock(|when, then| {
    when.method(POST)
    // below has extra fields such as 'client_order_id' which I don't want to validate
        .json_body_partial(r#"{
            "price":"23222.5",
            "size":"0.23",
        }"#);
    then.status(200).json_body(serde_json::json!({
        "data": {
             /* would be useful if this could come from the request */
            "client_order_id": "would_be_nice_to_access_this"
        }
    }));
});

Is this already possible? If not I'd be happy to attempt an implementation if it's not too difficult.

@nastynaz
Copy link
Author

I made this PR which allows for the request to be accessed when writing a .then response.

It's for my own use so I haven't added comments or tests, but if people find it useful I'm happy to edit it: #104

@alexliesenfeld
Copy link
Owner

alexliesenfeld commented Apr 12, 2024

Thanks for reaching out! It is indeed an interesting idea.

There is something similar for the request already (see here). It allows to dynamically evaluate if a request matches certain criteria at runtime. A dynamic body generator would probably look very similar.

Note though that httpmock also allows you to run a standalone mock server to which tests can connect to. Since it's not possible to transfer functions over the network, such dynamically defined response body generator would not work in standalone mode. A body generator can - just like matches - be disabled per cargo feature.

Thanks for your PR. I think it's good to wait for this feature at least for the next release to avoid having different solutions for the same problem.

@nastynaz
Copy link
Author

@alexliesenfeld that makes sense. I'm happy to wait as I've managed to get it running for my own use case.

I noticed that MockMatcherFunction is defined as an fn and not an Fn, was this by design? In my PR I changed it to be Fn so that I .matches() could accept closures which did other things like send the request data elsewhere.

@alexliesenfeld alexliesenfeld added the enhancement New feature or request label Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants