File: server_encode.rs

package info (click to toggle)
rust-async-h1 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 332 kB
  • sloc: sh: 11; makefile: 2
file content (167 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download
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
mod server_encode {
    use async_h1::server::Encoder;
    use async_std::io::Cursor;
    use async_std::io::ReadExt;
    use http_types::Body;
    use http_types::Result;
    use http_types::StatusCode;
    use http_types::{Method, Response};
    use pretty_assertions::assert_eq;

    async fn encode_to_string(
        response: Response,
        len: usize,
        method: Method,
    ) -> http_types::Result<String> {
        let mut buf = vec![];
        let mut encoder = Encoder::new(response, method);
        loop {
            let mut inner_buf = vec![0; len];
            let bytes = encoder.read(&mut inner_buf).await?;
            buf.extend_from_slice(&inner_buf[..bytes]);
            if bytes == 0 {
                return Ok(String::from_utf8(buf)?);
            }
        }
    }

    async fn assert_encoded(len: usize, method: Method, response: Response, lines: Vec<&str>) {
        assert_eq!(
            encode_to_string(response, len, method)
                .await
                .unwrap()
                .split("\r\n")
                .map(|line| {
                    if line.starts_with("date:") {
                        "date: {DATE}"
                    } else {
                        line
                    }
                })
                .collect::<Vec<_>>()
                .join("\r\n"),
            lines.join("\r\n")
        );
    }

    #[async_std::test]
    async fn basic() -> Result<()> {
        let res = Response::new(StatusCode::Ok);

        assert_encoded(
            100,
            Method::Get,
            res,
            vec![
                "HTTP/1.1 200 OK",
                "content-length: 0",
                "date: {DATE}",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }

    #[async_std::test]
    async fn basic_404() -> Result<()> {
        let res = Response::new(StatusCode::NotFound);

        assert_encoded(
            100,
            Method::Get,
            res,
            vec![
                "HTTP/1.1 404 Not Found",
                "content-length: 0",
                "date: {DATE}",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }

    #[async_std::test]
    async fn chunked() -> Result<()> {
        let mut res = Response::new(StatusCode::Ok);
        res.set_body(Body::from_reader(Cursor::new("hello world"), None));

        assert_encoded(
            10,
            Method::Get,
            res,
            vec![
                "HTTP/1.1 200 OK",
                "content-type: application/octet-stream",
                "date: {DATE}",
                "transfer-encoding: chunked",
                "",
                "5",
                "hello",
                "5",
                " worl",
                "1",
                "d",
                "0",
                "",
                "",
            ],
        )
        .await;
        Ok(())
    }

    #[async_std::test]
    async fn head_request_fixed_body() -> Result<()> {
        let mut res = Response::new(StatusCode::Ok);
        res.set_body("empty body because head request");

        assert_encoded(
            10,
            Method::Head,
            res,
            vec![
                "HTTP/1.1 200 OK",
                "content-length: 31",
                "content-type: text/plain;charset=utf-8",
                "date: {DATE}",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }

    #[async_std::test]
    async fn head_request_chunked_body() -> Result<()> {
        let mut res = Response::new(StatusCode::Ok);
        res.set_body(Body::from_reader(
            Cursor::new("empty body because head request"),
            None,
        ));

        assert_encoded(
            10,
            Method::Head,
            res,
            vec![
                "HTTP/1.1 200 OK",
                "content-type: application/octet-stream",
                "date: {DATE}",
                "transfer-encoding: chunked",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }
}