blob: 1886fdf897b691bd098c8be6c098d472be4695b7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
use chan::Sender;
use hyper::status::StatusCode;
use std::cell::RefCell;
use datatype::Error;
use http::{Client, Request, Response, ResponseData};
/// The `TestClient` will return HTTP responses from an existing list of strings.
pub struct TestClient {
responses: RefCell<Vec<String>>
}
impl Default for TestClient {
fn default() -> Self {
TestClient { responses: RefCell::new(Vec::new()) }
}
}
impl TestClient {
/// Create a new `TestClient` that will return these responses.
pub fn from(responses: Vec<String>) -> TestClient {
TestClient { responses: RefCell::new(responses) }
}
}
impl Client for TestClient {
fn chan_request(&self, req: Request, resp_tx: Sender<Response>) {
match self.responses.borrow_mut().pop() {
Some(body) => resp_tx.send(Response::Success(ResponseData {
code: StatusCode::Ok,
body: body.as_bytes().to_vec()
})),
None => resp_tx.send(Response::Error(Error::Client(req.url.to_string())))
}
}
fn is_testing(&self) -> bool { true }
}
|