File: network.rs

package info (click to toggle)
rust-tiny-http 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 416 kB
  • sloc: makefile: 2
file content (222 lines) | stat: -rw-r--r-- 6,815 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
extern crate tiny_http;

use std::io::{Read, Write};
use std::net::{Shutdown, TcpStream};
use std::thread;
use std::time::Duration;

#[allow(dead_code)]
mod support;

#[test]
fn connection_close_header() {
    let mut client = support::new_client_to_hello_world_server();

    (write!(client, "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n")).unwrap();
    thread::sleep(Duration::from_millis(1000));

    (write!(client, "GET / HTTP/1.1\r\nConnection: close\r\n\r\n")).unwrap();

    // if the connection was not closed, this will err with timeout
    // client.set_keepalive(Some(1)).unwrap(); FIXME: reenable this
    let mut out = Vec::new();
    client.read_to_end(&mut out).unwrap();
}

#[test]
fn http_1_0_connection_close() {
    let mut client = support::new_client_to_hello_world_server();

    (write!(client, "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n")).unwrap();

    // if the connection was not closed, this will err with timeout
    // client.set_keepalive(Some(1)).unwrap(); FIXME: reenable this
    let mut out = Vec::new();
    client.read_to_end(&mut out).unwrap();
}

#[test]
fn detect_connection_closed() {
    let mut client = support::new_client_to_hello_world_server();

    (write!(client, "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n")).unwrap();
    thread::sleep(Duration::from_millis(1000));

    client.shutdown(Shutdown::Write).unwrap();

    // if the connection was not closed, this will err with timeout
    // client.set_keepalive(Some(1)).unwrap(); FIXME: reenable this
    let mut out = Vec::new();
    client.read_to_end(&mut out).unwrap();
}

#[test]
fn poor_network_test() {
    let mut client = support::new_client_to_hello_world_server();

    (write!(client, "G")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "ET /he")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "llo HT")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "TP/1.")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "1\r\nHo")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "st: localho")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "st\r\nConnec")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "tion: close\r")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (write!(client, "\n\r")).unwrap();
    thread::sleep(Duration::from_millis(100));
    (writeln!(client)).unwrap();

    // client.set_keepalive(Some(2)).unwrap(); FIXME: reenable this
    let mut data = String::new();
    client.read_to_string(&mut data).unwrap();
    assert!(data.ends_with("hello world"));
}

#[test]
fn pipelining_test() {
    let mut client = support::new_client_to_hello_world_server();

    (write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")).unwrap();
    (write!(client, "GET /hello HTTP/1.1\r\nHost: localhost\r\n\r\n")).unwrap();
    (write!(
        client,
        "GET /world HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
    ))
    .unwrap();

    // client.set_keepalive(Some(2)).unwrap(); FIXME: reenable this
    let mut data = String::new();
    client.read_to_string(&mut data).unwrap();
    assert_eq!(data.split("hello world").count(), 4);
}

#[test]
fn server_crash_results_in_response() {
    let server = tiny_http::Server::http("0.0.0.0:0").unwrap();
    let port = server.server_addr().to_ip().unwrap().port();
    let mut client = TcpStream::connect(("127.0.0.1", port)).unwrap();

    thread::spawn(move || {
        server.recv().unwrap();
        // oops, server crash
    });

    (write!(
        client,
        "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
    ))
    .unwrap();

    // client.set_keepalive(Some(2)).unwrap(); FIXME: reenable this
    let mut content = String::new();
    client.read_to_string(&mut content).unwrap();
    assert!(&content[9..].starts_with('5')); // 5xx status code
}

#[test]
fn responses_reordered() {
    let (server, mut client) = support::new_one_server_one_client();

    (write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")).unwrap();
    (write!(
        client,
        "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
    ))
    .unwrap();

    thread::spawn(move || {
        let rq1 = server.recv().unwrap();
        let rq2 = server.recv().unwrap();

        thread::spawn(move || {
            rq2.respond(tiny_http::Response::from_string(
                "second request".to_owned(),
            ))
            .unwrap();
        });

        thread::sleep(Duration::from_millis(100));

        thread::spawn(move || {
            rq1.respond(tiny_http::Response::from_string("first request".to_owned()))
                .unwrap();
        });
    });

    // client.set_keepalive(Some(2)).unwrap(); FIXME: reenable this
    let mut content = String::new();
    client.read_to_string(&mut content).unwrap();
    assert!(content.ends_with("second request"));
}

#[test]
fn no_transfer_encoding_on_204() {
    let (server, mut client) = support::new_one_server_one_client();

    (write!(
        client,
        "GET / HTTP/1.1\r\nHost: localhost\r\nTE: chunked\r\nConnection: close\r\n\r\n"
    ))
    .unwrap();

    thread::spawn(move || {
        let rq = server.recv().unwrap();

        let resp = tiny_http::Response::empty(tiny_http::StatusCode(204));
        rq.respond(resp).unwrap();
    });

    let mut content = String::new();
    client.read_to_string(&mut content).unwrap();

    assert!(content.starts_with("HTTP/1.1 204"));
    assert!(!content.contains("Transfer-Encoding: chunked"));
}

/* FIXME: uncomment and fix
#[test]
fn connection_timeout() {
    let (server, mut client) = {
        let server = tiny_http::ServerBuilder::new()
            .with_client_connections_timeout(3000)
            .with_random_port().build().unwrap();
        let port = server.server_addr().port();
        let client = TcpStream::connect(("127.0.0.1", port)).unwrap();
        (server, client)
    };

    let (tx_stop, rx_stop) = mpsc::channel();

    // executing server in parallel
    thread::spawn(move || {
        loop {
            server.try_recv();
            thread::sleep(Duration::from_millis(100));
            if rx_stop.try_recv().is_ok() { break }
        }
    });

    // waiting for the 408 response
    let mut content = String::new();
    client.read_to_string(&mut content).unwrap();
    assert!(&content[9..].starts_with("408"));

    // stopping server
    tx_stop.send(());
}
*/

#[test]
fn chunked_threshold() {
    let resp = tiny_http::Response::from_string("test".to_string());
    assert_eq!(resp.chunked_threshold(), 32768);
    assert_eq!(resp.with_chunked_threshold(42).chunked_threshold(), 42);
}