alexliesenfeld / httpmock

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

Mock.expect_body and binary data #12

Closed Ch00k closed 3 years ago

Ch00k commented 3 years ago

Is there any way I can use Mock.expect_body with binary data (e.g. when I send POST request for a file upload)?

alexliesenfeld commented 3 years ago

There are no special mechanisms in place for file upload yet. If expect_body is too limiting and if you don't use httpmock in standalone mode you can try to use the generic matcher function like this:

.expect_match(|req: MockServerRequest| { req.body.as_ref().unwrap().eq("test") })

where test is the file content in string representation. It is not a closure at this time though but a simple function.

There is also an example here: https://github.com/alexliesenfeld/httpmock/blob/master/tests/request_matcher_tests.rs#L40

A PR for this is very welcome 😃.

Ch00k commented 3 years ago

I also tried this, and it works:

let mock_server = MockServer::start();

Mock::new()
    .expect_method(Method::POST)
    .expect_path("/foo")
    .expect_body("bar")
    .create_on(&mock_server);

let client = reqwest::blocking::Client::new();
client
    .post(&mock_server.url("/foo"))
    .body("bar".as_bytes())
    .send()
    .unwrap();
alexliesenfeld commented 3 years ago

@Ch00k Is this still an issue?

Ch00k commented 3 years ago

No, sorry, I forgot to close it :)