leoschwarz / reqwest_mock Goto Github PK
View Code? Open in Web Editor NEWProvides a mockable wrapper around the reqwest HTTP client for Rust.
License: Apache License 2.0
Provides a mockable wrapper around the reqwest HTTP client for Rust.
License: Apache License 2.0
Wondering if you agree that this would be nice, in which case I can try to implement it.
Looking at the StubClient
, it seems only able to serve a given static response for given request parameters (URL etc).
I find myself in a somewhat more complex situation where I would like to mock a service which gives a response that depends on the request (e.g. a field from the request body would appear in the response body).
So my suggestion is for ResponseStubber
to have a function like
pub fn body_with<B: Into<Body>, F: Fn(Method, Body, HeaderMap) -> B>(self, body: F) -> Self
Or should I create a new client struct (not StubClient) for this?
Evaluate whether there is a need to disable mocking with a cargo feature.
The main use case I would see is a crate using reqwest_mock internally (i.e. not exporting the mockability to API users) to test the crate with a cfg(test)
. In this case removing all mock functionality might remove binary size (removing some dependencies) and depending on the optimizations which are made in reqwest_mock also speed up the final binary.
What would speak against such a feature is that it would obviously make the code base more complicated.
There should be integration tests (inside tests
directory) for all clients.
Store the version number of reqwest_mock in the replay files and only deserialize files with the correct version number.
Evaluate how this check of the version number can be performed using serde before deserializing the whole thing, so that this will be future proof and we don't have to worry about breaking the format in the future that much.
Design an API that provides a way to specify just a directory and everything will be handled automatically.
Open question:
I guess this is mostly work for when the other issues are resolved but still not unimportant either
Client
.WIP
The http crate provides fundamental http types. In general it might be cool if as much of the API here could be replaced with code from that crate since this would increase interoperability with other crates.
Currently bytes will be written into a json array of numbers, which for larger requests could mean a significant overhead. Either evaluate an alternative to json (like msgpack or something similar) or improve the serialization somehow.
I think especially GenericClient
might be confusing and ugly in api user's code.
let client2 = ReplayClient::new(RecordingTarget::file("simple.replay"));
let resp1 = perform_request(&client2, URL);
let resp2 = perform_request(&client2, URL);
assert_eq!(resp1, resp2);
The client use reqwest::blocking
internally, which as noted in the docs can't be used in async contexts. I've just hit this in palfrey/dawn#32 trying to work with the actix test server, which wants everything to be async.
It would be really useful to have a read-only mode for ReplayClient. This means you can do things like write a bunch of responses in local testing, commit them to source control, and then enable read-only mode in CI so it only uses the responses that are already written.
I've used features like this in VCR.py which has a variety of useful "record modes" like this
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.