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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use ::anyhow::anyhow;
use ::anyhow::Context;
use ::anyhow::Result;
use ::auto_future::AutoFuture;
use ::hyper::body::to_bytes;
use ::hyper::body::Body;
use ::hyper::body::Bytes;
use ::hyper::header;
use ::hyper::http::header::SET_COOKIE;
use ::hyper::http::Request;
use ::hyper::Client;
use ::serde::Serialize;
use ::serde_json::to_vec as json_to_vec;
use ::std::convert::AsRef;
use ::std::fmt::Debug;
use ::std::fmt::Write;
use ::std::future::IntoFuture;
use ::std::sync::Arc;
use ::std::sync::Mutex;
use axum::http::HeaderValue;
use hyper::header::HeaderName;
use crate::InnerTestServer;
use crate::TestResponse;
mod test_request_config;
pub(crate) use self::test_request_config::*;
mod test_request_details;
pub(crate) use self::test_request_details::*;
const JSON_CONTENT_TYPE: &'static str = &"application/json";
const TEXT_CONTENT_TYPE: &'static str = &"text/plain";
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct TestRequest {
details: TestRequestDetails,
inner_test_server: Arc<Mutex<InnerTestServer>>,
full_request_path: String,
body: Option<Body>,
headers: Vec<(HeaderName, HeaderValue)>,
content_type: Option<String>,
is_expecting_failure: bool,
is_saving_cookies: bool,
}
impl TestRequest {
pub(crate) fn new(
inner_test_server: Arc<Mutex<InnerTestServer>>,
config: TestRequestConfig,
details: TestRequestDetails,
) -> Result<Self> {
let server_locked = inner_test_server.as_ref().lock().map_err(|err| {
anyhow!(
"Failed to lock InternalTestServer for {} {}, received {:?}",
details.method,
details.path,
err
)
})?;
let full_request_path = build_request_path(server_locked.server_address(), &details.path);
let cookie_header_raw =
server_locked
.cookies()
.iter()
.fold(String::new(), |mut buffer, cookie| {
if buffer.len() > 0 {
write!(buffer, "; ").expect(
"Writing to internal string for cookie header should always work",
);
}
write!(buffer, "{}", cookie)
.expect("Writing to internal string for cookie header should always work");
buffer
});
::std::mem::drop(server_locked);
let mut initial_headers: Vec<(HeaderName, HeaderValue)> = vec![];
if cookie_header_raw.len() > 0 {
let header_value = HeaderValue::from_str(&cookie_header_raw)?;
initial_headers.push((header::COOKIE, header_value));
}
Ok(Self {
details,
inner_test_server,
full_request_path,
body: None,
headers: initial_headers,
content_type: config.content_type,
is_expecting_failure: false,
is_saving_cookies: config.save_cookies,
})
}
pub fn do_save_cookies(mut self) -> Self {
self.is_saving_cookies = true;
self
}
pub fn do_not_save_cookies(mut self) -> Self {
self.is_saving_cookies = false;
self
}
pub fn expect_fail(mut self) -> Self {
self.is_expecting_failure = true;
self
}
pub fn json<J>(mut self, body: &J) -> Self
where
J: Serialize,
{
let body_bytes = json_to_vec(body).expect("It should serialize the content into JSON");
let body: Body = body_bytes.into();
self.body = Some(body);
if self.content_type == None {
self.content_type = Some(JSON_CONTENT_TYPE.to_string());
}
self
}
pub fn text<S>(mut self, raw_body: S) -> Self
where
S: AsRef<str>,
{
let body_bytes = Bytes::copy_from_slice(raw_body.as_ref().as_bytes());
if self.content_type == None {
self.content_type = Some(TEXT_CONTENT_TYPE.to_string());
}
self.bytes(body_bytes)
}
pub fn bytes(mut self, body_bytes: Bytes) -> Self {
let body: Body = body_bytes.into();
self.body = Some(body);
self
}
pub fn content_type(mut self, content_type: &str) -> Self {
self.content_type = Some(content_type.to_string());
self
}
async fn send_or_panic(self) -> TestResponse {
self.send().await.expect("Sending request failed")
}
async fn send(mut self) -> Result<TestResponse> {
let path = self.details.path;
let save_cookies = self.is_saving_cookies;
let body = self.body.unwrap_or(Body::empty());
let mut request_builder = Request::builder()
.uri(&self.full_request_path)
.method(self.details.method);
let mut headers = self.headers;
if let Some(content_type) = self.content_type {
let header = build_content_type_header(content_type)?;
headers.push(header);
}
for (header_name, header_value) in headers {
request_builder = request_builder.header(header_name, header_value);
}
let request = request_builder.body(body).with_context(|| {
format!(
"Expect valid hyper Request to be built on request to {}",
path
)
})?;
let hyper_response = Client::new()
.request(request)
.await
.with_context(|| format!("Expect Hyper Response to succeed on request to {}", path))?;
let (parts, response_body) = hyper_response.into_parts();
let response_bytes = to_bytes(response_body).await?;
if save_cookies {
let cookie_headers = parts.headers.get_all(SET_COOKIE).into_iter();
InnerTestServer::add_cookies_by_header(&mut self.inner_test_server, cookie_headers)?;
}
let mut response = TestResponse::new(path, parts, response_bytes);
if self.is_expecting_failure {
response = response.assert_status_not_ok();
} else {
response = response.assert_status_ok();
}
Ok(response)
}
}
impl IntoFuture for TestRequest {
type Output = TestResponse;
type IntoFuture = AutoFuture<TestResponse>;
fn into_future(self) -> Self::IntoFuture {
let raw_future = self.send_or_panic();
AutoFuture::new(raw_future)
}
}
fn build_request_path(root_path: &str, sub_path: &str) -> String {
if sub_path == "" {
return format!("http://{}", root_path.to_string());
}
if sub_path.starts_with("/") {
return format!("http://{}{}", root_path, sub_path);
}
format!("http://{}/{}", root_path, sub_path)
}
fn build_content_type_header(content_type: String) -> Result<(HeaderName, HeaderValue)> {
let header_value = HeaderValue::from_str(&content_type)
.with_context(|| format!("Failed to store header content type '{}'", content_type))?;
Ok((header::CONTENT_TYPE, header_value))
}