reqwest_mock

Provides a mockable reqwest-like HTTP client.

Write your code generic over the Client trait, and in production use DirectClient while in testing you can use ReplayClient, which will record a request the first time and replay it every time the exact same request is made in the future.

Examples

```rust use reqwestmock::{Client, DirectClient, ReplayClient, Error}; use reqwestmock::header::UserAgent;

struct MyClient { client: C, }

fn new_client() -> MyClient { MyClient { client: DirectClient::new() } }

[cfg(test)]

fn test_client(path: &str) -> MyClient { MyClient { client: ReplayClient::new(path) } }

impl MyClient { /// For simplicity's sake we are not parsing the response but just extracting the /// response body. /// Also in your own code it might be a good idea to define your own Error type. pub fn gettime(&self) -> Result { let response = self.client .get("https://now.httpbin.org/") .header(UserAgent("MyClient".tostring())) .send()?;

    response.body_to_utf8()
}

} ```