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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::test_client::test_request::TestRequest;
use crate::{App, Method, State};
use hyper::{http, Uri};
use std::sync::Arc;
mod test_request;
mod test_response;
/// A client that can send fake requests to an App and receive the responses back for unit
/// and integration testing. Obtain one by calling [App::test]
pub struct TestClient<S: State> {
app: Arc<App<S>>,
}
impl<S: State> TestClient<S> {
pub(crate) fn new(app: App<S>) -> Self {
Self { app: Arc::new(app) }
}
/// Prepare a GET request. Returns a TestRequest which is used to add headers and the body
/// before being sent.
pub fn get<U>(&self, uri: U) -> TestRequest<S>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<hyper::http::Error>,
{
self.method(Method::GET, uri)
}
/// Prepare a POST request. Returns a TestRequest which is used to add headers and the body
/// before being sent.
pub fn post<U>(&self, uri: U) -> TestRequest<S>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<hyper::http::Error>,
{
self.method(Method::POST, uri)
}
/// Prepare a PUT request. Returns a TestRequest which is used to add headers and the body
/// before being sent.
pub fn put<U>(&self, uri: U) -> TestRequest<S>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<hyper::http::Error>,
{
self.method(Method::PUT, uri)
}
/// Prepare a DELETE request. Returns a TestRequest which is used to add headers and the body
/// before being sent.
pub fn delete<U>(&self, uri: U) -> TestRequest<S>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<hyper::http::Error>,
{
self.method(Method::DELETE, uri)
}
/// Prepare request with the given HTTP method. Returns a TestRequest which is used to add headers
/// and the body before being sent.
pub fn method<U>(&self, method: Method, uri: U) -> TestRequest<S>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<hyper::http::Error>,
{
TestRequest::new(
self.app.clone(),
http::request::Builder::new().method(method).uri(uri),
)
}
}