1
//! Helper functions for the directory client code
2

            
3
/// Encode an HTTP request in a quick and dirty HTTP 1.0 format.
4
17
pub(crate) fn encode_request(req: &http::Request<()>) -> String {
5
17
    let mut s = format!("{} {} HTTP/1.0\r\n", req.method(), req.uri());
6

            
7
18
    for (key, val) in req.headers().iter() {
8
18
        s.push_str(&format!(
9
18
            "{}: {}\r\n",
10
18
            key,
11
18
            val.to_str()
12
18
                .expect("Added an HTTP header that wasn't UTF-8!")
13
18
        ));
14
18
    }
15
17
    s.push_str("\r\n");
16
17
    s
17
17
}
18

            
19
#[cfg(test)]
20
mod test {
21
    #![allow(clippy::unwrap_used)]
22
    use super::*;
23

            
24
    #[test]
25
    fn format() {
26
        let req = http::Request::builder()
27
            .method("GET")
28
            .uri("/index.html")
29
            .body(())
30
            .unwrap();
31
        assert_eq!(encode_request(&req), "GET /index.html HTTP/1.0\r\n\r\n");
32
        let req = http::Request::builder()
33
            .method("GET")
34
            .uri("/index.html")
35
            .header("X-Marsupial", "Opossum")
36
            .body(())
37
            .unwrap();
38
        assert_eq!(
39
            encode_request(&req),
40
            "GET /index.html HTTP/1.0\r\nx-marsupial: Opossum\r\n\r\n"
41
        );
42
    }
43
}