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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use ::anyhow::Context;
use ::anyhow::Result;
use ::axum::routing::IntoMakeService;
use ::axum::Router;
use ::cookie::Cookie;
use ::cookie::CookieJar;
use ::hyper::http::Method;
use ::std::sync::Arc;
use ::std::sync::Mutex;
use crate::TestRequest;
use crate::TestServerConfig;
mod inner_test_server;
pub(crate) use self::inner_test_server::*;
#[derive(Debug)]
pub struct TestServer {
inner: Arc<Mutex<InnerTestServer>>,
}
impl TestServer {
pub fn new(app: IntoMakeService<Router>) -> Result<Self> {
Self::new_with_config(app, TestServerConfig::default())
}
pub fn new_with_config(
app: IntoMakeService<Router>,
options: TestServerConfig,
) -> Result<Self> {
let inner_test_server = InnerTestServer::new(app, options)?;
let inner_mutex = Mutex::new(inner_test_server);
let inner = Arc::new(inner_mutex);
Ok(Self { inner })
}
pub fn clear_cookies(&mut self) {
InnerTestServer::clear_cookies(&mut self.inner)
.with_context(|| format!("Trying to clear_cookies"))
.unwrap()
}
pub fn add_cookies(&mut self, cookies: CookieJar) {
InnerTestServer::add_cookies(&mut self.inner, cookies)
.with_context(|| format!("Trying to add_cookies"))
.unwrap()
}
pub fn add_cookie(&mut self, cookie: Cookie) {
InnerTestServer::add_cookie(&mut self.inner, cookie)
.with_context(|| format!("Trying to add_cookie"))
.unwrap()
}
pub fn get(&self, path: &str) -> TestRequest {
self.method(Method::GET, path)
}
pub fn post(&self, path: &str) -> TestRequest {
self.method(Method::POST, path)
}
pub fn patch(&self, path: &str) -> TestRequest {
self.method(Method::PATCH, path)
}
pub fn put(&self, path: &str) -> TestRequest {
self.method(Method::PUT, path)
}
pub fn delete(&self, path: &str) -> TestRequest {
self.method(Method::DELETE, path)
}
pub fn method(&self, method: Method, path: &str) -> TestRequest {
let debug_method = method.clone();
InnerTestServer::send(&self.inner, method, path)
.with_context(|| {
format!(
"Trying to create internal request for {} {}",
debug_method, path
)
})
.unwrap()
}
}