In other to be able to test a CLI that makes use of reqwest
to make http calls, I am trying to abstract request
so I can replace it in the test.
So far, I have defined a trait as follow:
#[async_trait]
pub trait HttpClient {
async fn send_request(&self, path: String) -> Result<Response, Error>;
}
with an implementation for request
as follows:
#[async_trait]
impl HttpClient for reqwest::Client {
async fn send_request(&self, path: String) -> Result<Response, Error> {
self.get(path).send().await
}
}
This works fine and I can confirm the cli still works as it should.
Now in the test, I am stuck trying to create a mock implementation of the HttpClient
as I am not sure how to construct a request::Response
manually.
So far in the test I have this:
struct MockClient {
res_req: HashMap<String, Result<Response, Error>>
}
impl MockClient {
pub fn mock(res_req: HashMap<String, Result<Response, Error>>) -> Self {
Self {
res_req,
}
}
}
#[async_trait]
impl HttpClient for MockClient {
async fn send_request(&self, path: String) -> Result<Response, Error> {
self.res_req.get(path).unwrap()
}
}
but then when I need to create an instance, not sure how to create request::Response
manually. That is
let client = MockClient::mock(
HashMap::from(
[("path-1".to_string(), ???)]
));
Any ideas?