forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasserts.rs
More file actions
153 lines (134 loc) · 5.83 KB
/
Copy pathasserts.rs
File metadata and controls
153 lines (134 loc) · 5.83 KB
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
use reqwest::Response;
use super::responses::announce::{Announce, Compact, DeserializedCompact};
use super::responses::scrape;
use crate::http::responses::error::Error;
pub async fn assert_empty_announce_response(response: Response) {
assert_eq!(response.status(), 200);
let announce_response: Announce = serde_bencode::from_str(&response.text().await.unwrap()).unwrap();
assert!(announce_response.peers.is_empty());
}
pub async fn assert_announce_response(response: Response, expected_announce_response: &Announce) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let announce_response: Announce = serde_bencode::from_str(&body)
.unwrap_or_else(|_| panic!("response body should be a valid announce response, got \"{}\"", &body));
assert_eq!(announce_response, *expected_announce_response);
}
/// Sample bencoded announce response as byte array:
///
/// ```text
/// b"d8:intervali120e12:min intervali120e8:completei2e10:incompletei0e5:peers6:~\0\0\x01\x1f\x90e6:peers60:e"
/// ```
pub async fn assert_compact_announce_response(response: Response, expected_response: &Compact) {
assert_eq!(response.status(), 200);
let bytes = response.bytes().await.unwrap();
let compact_announce = DeserializedCompact::from_bytes(&bytes).unwrap_or_else(|_| {
panic!(
"response body should be a valid compact announce response, got \"{:?}\"",
&bytes
)
});
let actual_response = Compact::from(compact_announce);
assert_eq!(actual_response, *expected_response);
}
/// Sample bencoded scrape response as byte array:
///
/// ```text
/// b"d5:filesd20:\x9c8B\"\x13\xe3\x0b\xff!+0\xc3`\xd2o\x9a\x02\x13d\"d8:completei1e10:downloadedi0e10:incompletei0eeee"
/// ```
pub async fn assert_scrape_response(response: Response, expected_response: &scrape::Response) {
assert_eq!(response.status(), 200);
let scrape_response = scrape::Response::try_from_bencoded(&response.bytes().await.unwrap()).unwrap();
assert_eq!(scrape_response, *expected_response);
}
pub async fn assert_is_announce_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let _announce_response: Announce = serde_bencode::from_str(&body)
.unwrap_or_else(|_| panic!("response body should be a valid announce response, got \"{}\"", &body));
}
// Error responses
pub async fn assert_internal_server_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'internal server' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "internal server error".to_string(),
};
assert_eq!(error_response, expected_error_response);
}
pub async fn assert_invalid_info_hash_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'invalid info_hash' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "info_hash is either missing or invalid".to_string(),
};
assert_eq!(error_response, expected_error_response);
}
pub async fn assert_invalid_peer_id_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'invalid peer id' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "peer_id is either missing or invalid".to_string(),
};
assert_eq!(error_response, expected_error_response);
}
pub async fn assert_torrent_not_in_whitelist_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'torrent not on whitelist' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "torrent not on whitelist".to_string(),
};
assert_eq!(error_response, expected_error_response);
}
pub async fn assert_peer_not_authenticated_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'peer not authenticated' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "peer not authenticated".to_string(),
};
assert_eq!(error_response, expected_error_response);
}
pub async fn assert_invalid_authentication_key_error_response(response: Response) {
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| {
panic!(
"response body should be a valid bencoded string for the 'invalid authentication key' error, got \"{}\"",
&body
)
});
let expected_error_response = Error {
failure_reason: "invalid authentication key".to_string(),
};
assert_eq!(error_response, expected_error_response);
}