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
use ::anyhow::Context;
use ::cookie::Cookie;
use ::cookie::CookieJar;
use ::hyper::body::Bytes;
use ::hyper::http::header::AsHeaderName;
use ::hyper::http::header::HeaderName;
use ::hyper::http::header::SET_COOKIE;
use ::hyper::http::response::Parts;
use ::hyper::http::HeaderMap;
use ::hyper::http::HeaderValue;
use ::hyper::http::StatusCode;
use ::serde::Deserialize;
use ::std::convert::AsRef;
use ::std::fmt::Debug;
#[derive(Clone, Debug)]
pub struct TestResponse {
request_url: String,
headers: HeaderMap<HeaderValue>,
status_code: StatusCode,
response_body: Bytes,
}
impl TestResponse {
pub(crate) fn new(request_url: String, parts: Parts, response_body: Bytes) -> Self {
Self {
request_url,
headers: parts.headers,
status_code: parts.status,
response_body,
}
}
#[must_use]
pub fn request_url<'a>(&'a self) -> &'a str {
&self.request_url
}
#[must_use]
pub fn bytes<'a>(&'a self) -> &'a [u8] {
&self.response_body
}
#[must_use]
pub fn text(&self) -> String {
String::from_utf8_lossy(&self.response_body).to_string()
}
#[must_use]
pub fn status_code(&self) -> StatusCode {
self.status_code
}
#[must_use]
pub fn header<N>(&self, header_name: N) -> Option<HeaderValue>
where
N: AsHeaderName,
{
self.headers.get(header_name).map(|h| h.to_owned())
}
pub fn iter_headers<'a>(&'a self) -> impl Iterator<Item = (&'a HeaderName, &'a HeaderValue)> {
self.headers.iter()
}
pub fn iter_headers_by_name<'a, N>(
&'a self,
header_name: N,
) -> impl Iterator<Item = &'a HeaderValue>
where
N: AsHeaderName,
{
self.headers.get_all(header_name).iter()
}
#[must_use]
pub fn cookie(&self, cookie_name: &str) -> Cookie<'static> {
self.maybe_cookie(cookie_name)
.with_context(|| {
format!(
"Cannot find cookie {} for response {}",
cookie_name, self.request_url
)
})
.unwrap()
}
#[must_use]
pub fn maybe_cookie(&self, cookie_name: &str) -> Option<Cookie<'static>> {
for cookie in self.iter_cookies() {
if cookie.name() == cookie_name {
return Some(cookie.into_owned());
}
}
None
}
#[must_use]
pub fn cookies(&self) -> CookieJar {
let mut cookies = CookieJar::new();
for cookie in self.iter_cookies() {
cookies.add(cookie.into_owned());
}
cookies
}
pub fn iter_cookies<'a>(&'a self) -> impl Iterator<Item = Cookie<'a>> {
self.iter_headers_by_name(SET_COOKIE).map(|header| {
let header_str = header
.to_str()
.with_context(|| {
format!(
"Reading header 'Set-Cookie' as string for response {}",
self.request_url
)
})
.unwrap();
Cookie::parse(header_str)
.with_context(|| {
format!(
"Parsing 'Set-Cookie' header for response {}",
self.request_url
)
})
.unwrap()
})
}
#[must_use]
pub fn json<T>(&self) -> T
where
for<'de> T: Deserialize<'de>,
{
serde_json::from_slice::<T>(&self.response_body)
.with_context(|| {
format!(
"Deserializing response from JSON for request {}",
self.request_url
)
})
.unwrap()
}
pub fn assert_text<C>(self, other: C) -> Self
where
C: AsRef<str>,
{
let other_contents = other.as_ref();
assert_eq!(&self.text(), other_contents);
self
}
pub fn assert_json<T>(self, other: &T) -> Self
where
for<'de> T: Deserialize<'de> + PartialEq<T> + Debug,
{
let own_json: T = self.json();
assert_eq!(own_json, *other);
self
}
pub fn assert_status_bad_request(self) -> Self {
self.assert_status(StatusCode::BAD_REQUEST)
}
pub fn assert_status_not_found(self) -> Self {
self.assert_status(StatusCode::NOT_FOUND)
}
pub fn assert_status_ok(self) -> Self {
self.assert_status(StatusCode::OK)
}
pub fn assert_status_not_ok(self) -> Self {
self.assert_not_status(StatusCode::OK)
}
pub fn assert_status(self, status_code: StatusCode) -> Self {
assert_eq!(self.status_code(), status_code);
self
}
pub fn assert_not_status(self, status_code: StatusCode) -> Self {
assert_ne!(self.status_code(), status_code);
self
}
}