File: client_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 (180 lines) | stat: -rw-r--r-- 4,988 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
168
169
170
171
172
173
174
175
176
177
178
179
180
mod client_encode {
    use async_h1::client;
    use async_std::io::Cursor;
    use async_std::prelude::*;
    use client::Encoder;
    use http_types::Body;
    use http_types::Result;
    use http_types::{Method, Request, Url};
    use pretty_assertions::assert_eq;

    async fn encode_to_string(request: Request, len: usize) -> http_types::Result<String> {
        let mut buf = vec![];
        let mut encoder = Encoder::new(request);
        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_std::test]
    async fn client_encode_request_add_date() -> Result<()> {
        let url = Url::parse("http://localhost:8080").unwrap();
        let mut req = Request::new(Method::Post, url);
        req.set_body("hello");

        assert_encoded(
            100,
            req,
            vec![
                "POST / HTTP/1.1",
                "host: localhost:8080",
                "content-length: 5",
                "content-type: text/plain;charset=utf-8",
                "",
                "hello",
            ],
        )
        .await;
        Ok(())
    }

    #[async_std::test]
    async fn client_encode_request_with_connect() -> Result<()> {
        let url = Url::parse("https://example.com:443").unwrap();
        let req = Request::new(Method::Connect, url);

        assert_encoded(
            100,
            req,
            vec![
                "CONNECT example.com:443 HTTP/1.1",
                "host: example.com",
                "content-length: 0",
                "proxy-connection: keep-alive",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }

    // The fragment of an URL is not send to the server, see RFC7230 and RFC3986.
    #[async_std::test]
    async fn client_encode_request_with_fragment() -> Result<()> {
        let url = Url::parse("http://example.com/path?query#fragment").unwrap();
        let req = Request::new(Method::Get, url);

        assert_encoded(
            10,
            req,
            vec![
                "GET /path?query HTTP/1.1",
                "host: example.com",
                "content-length: 0",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }

    async fn assert_encoded(len: usize, req: Request, lines: Vec<&str>) {
        assert_eq!(
            encode_to_string(req, len).await.unwrap(),
            lines.join("\r\n"),
        )
    }

    #[async_std::test]
    async fn client_encode_chunked_body() -> Result<()> {
        let url = Url::parse("http://example.com/path?query").unwrap();
        let mut req = Request::new(Method::Get, url.clone());
        req.set_body(Body::from_reader(Cursor::new("hello world"), None));

        assert_encoded(
            10,
            req,
            vec![
                "GET /path?query HTTP/1.1",
                "host: example.com",
                "content-type: application/octet-stream",
                "transfer-encoding: chunked",
                "",
                "5",
                "hello",
                "5",
                " worl",
                "1",
                "d",
                "0",
                "",
                "",
            ],
        )
        .await;

        let mut req = Request::new(Method::Get, url.clone());
        req.set_body(Body::from_reader(Cursor::new("hello world"), None));

        assert_encoded(
            16,
            req,
            vec![
                "GET /path?query HTTP/1.1",
                "host: example.com",
                "content-type: application/octet-stream",
                "transfer-encoding: chunked",
                "",
                "B",
                "hello world",
                "0",
                "",
                "",
            ],
        )
        .await;

        let mut req = Request::new(Method::Get, url.clone());
        req.set_body(Body::from_reader(
            Cursor::new(
                "this response is more than 32 bytes long in order to require a second hex digit",
            ),
            None,
        ));

        assert_encoded(
            32,
            req,
            vec![
                "GET /path?query HTTP/1.1",
                "host: example.com",
                "content-type: application/octet-stream",
                "transfer-encoding: chunked",
                "",
                "1A",
                "this response is more than",
                "1A",
                " 32 bytes long in order to",
                "1A",
                " require a second hex digi",
                "1",
                "t",
                "0",
                "",
                "",
            ],
        )
        .await;

        Ok(())
    }
}