blob: dfd37561f0a9b1c45ff00a485c31ae252d8b50bb (
plain)
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
|
//! Helper functions for the directory client code
use std::fmt::Write;
/// Encode an HTTP request in a quick and dirty HTTP 1.0 format.
pub(crate) fn encode_request(req: &http::Request<()>) -> String {
let mut s = format!("{} {} HTTP/1.0\r\n", req.method(), req.uri());
for (key, val) in req.headers().iter() {
write!(
s,
"{}: {}\r\n",
key,
val.to_str()
.expect("Added an HTTP header that wasn't UTF-8!")
)
.unwrap();
}
s.push_str("\r\n");
s
}
#[cfg(test)]
mod test {
// @@ begin test lint list maintained by maint/add_warning @@
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
#[test]
fn format() {
let req = http::Request::builder()
.method("GET")
.uri("/index.html")
.body(())
.unwrap();
assert_eq!(encode_request(&req), "GET /index.html HTTP/1.0\r\n\r\n");
let req = http::Request::builder()
.method("GET")
.uri("/index.html")
.header("X-Marsupial", "Opossum")
.body(())
.unwrap();
assert_eq!(
encode_request(&req),
"GET /index.html HTTP/1.0\r\nx-marsupial: Opossum\r\n\r\n"
);
}
}
|