-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Testing with Reqwest #154
Comments
https://docs.rs/crate/reqwest_mock/ perhaps? I haven't used it myself. |
Hi, I'm the author of that crate. Basically it defines the trait As for structs implementing That said my two main reasons not to promote my crate yet is that having your whole client code be generic is potentially annoying and I want to add a useful type providing boxing optimized for the |
I'm super keen on this. Is the best way to help to try https://github.com/leoschwarz/reqwest_mock and report any sharp edges? |
It's reasonable to have third-party crates implement different kinds of mocks, because different people will want to test their code in different ways. However, I feel like |
Right now So, it would be great if there was some kind of |
@pwoolcoc You are right that this is probably the best spot to conduct the mocking and not being able to create a response instance (and serializing/deserializing requests and responses for the replay client) has lead me to the current api duplication approach (I'm sorry but I don't have the resources to update it at this time). I think by now everything minus the serialization is handled by the http crate (there is |
@leoschwarz no worries, I understand. are EDIT: just saw your edit, yea I'll have to look into it some more. I know reqwest uses the http crate for Headers, not sure what else though |
I find mockito to be a fairly rustic solution so far. It spins up a web server for the lifetime of the test, and you use the #[cfg(test)]
use mockito;
// ...
// The host to be used for non-test (production) compilation
#[cfg(not(test))]
let host = "http://example.com";
// The host to be used in test compilation
#[cfg(test)]
let host = &mockito::server_url();
let url = format!("{}/endpoint", host);
let text = reqwest::get(&url)?.text()?; The advantage of this approach is that it works with any HTTP client library, and does not require any change to existing libraries. |
Hey, any updates on that? I was thinking about creating a project that wraps reqwest with a fluent API for unit tests with mocking. This project would support any kind of library for making HTTP requests, but reqwest would be the default implementation. It would be a kind of wrapper that behaves differently depending on the environment. So in test code, it could be a mocked struct which implements the exact same API of non-test code, but with additional methods for configuring HTTP calls behavior; something like this: #[test]
fn my_test() {
let mut wrapper = Wrapper::new(); // the name doesn't matter yet
wrapper.mock()
.get()
.from_url("https://example.com")
.to_status_code(200)
.done();
wrapper.get("https://example.com");
// then here we can access the wrapper object to get info about the requests made
} So I was wondering if it'd be a better idea to implement that as a feature of the reqwest instead. 🤔 If you guys agree with me, I can try to do it inside this project. Or is it better to keep it in a separate project? What do you think? |
i don't like the idea, of spinning up a webserver to implement unit tests. If this is required you may as well call it an integration test and test more broadly what i did was just create a trait around a client that uses reqwest. Then made sure the client had minimal logic as its not unit tested. Then i can use mockall to services above it. However, if the reqwest library creates a trait, then it would be easier to mock https://docs.rs/mockall/0.7.2/mockall/#external-traits and test at a lower level of my code without relying on integration tests. |
I've discontinued reqwest_mock, but since this issue is still open I would like to point to these potentially relevant crates for anyone who comes accross this issue in the future: |
Note that there is reqwest/src/async_impl/response.rs Lines 431 to 443 in 17c893f
|
As a word of caution, the solution by @lambda-fairy works only for |
I think it doesn't matter whether you need to build an http 0.2 response type or a custom reqwest response type though I assume reqwest uses that type internally anyways. Also there is an open PR that updates http to 1.0 so at that point it should interoperate with the whole http 1.0 ecosysten |
Any news on that? It's a shame there is no trait to mock for unit testing |
When I run unit tests I would like reqwest to return setup data instead of making the actual request. So, Is there a way to stub out reqwest to return canned responses for unit testing?
The text was updated successfully, but these errors were encountered: