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
use ::anyhow::Context;
use ::axum::http::StatusCode;
use ::serde::Deserialize;
use ::std::convert::AsRef;
use ::std::fmt::Debug;
pub struct TestResponse {
pub request_url: String,
pub contents: String,
pub status_code: StatusCode,
}
impl TestResponse {
pub(crate) fn new(request_url: String, contents: String, status_code: StatusCode) -> Self {
Self {
request_url,
contents,
status_code,
}
}
pub fn request_url(&self) -> String {
self.request_url.clone()
}
pub fn contents(&self) -> String {
self.contents.clone()
}
pub fn status_code(&self) -> StatusCode {
self.status_code
}
pub fn json<T>(&self) -> T
where
for<'de> T: Deserialize<'de>,
{
serde_json::from_str::<T>(&self.contents)
.with_context(|| {
format!(
"Deserializing response from JSON for request {}",
self.request_url
)
})
.unwrap()
}
pub fn assert_contents<C>(self, other: C) -> Self
where
C: AsRef<str>,
{
let other_contents = other.as_ref();
assert_eq!(&self.contents, 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
}
}