forked from tiny-http/tiny-http
/
input-tests.rs
95 lines (72 loc) · 3.03 KB
/
input-tests.rs
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
extern crate tiny_http;
use std::io::{Read, Write};
use std::sync::mpsc;
use std::net::Shutdown;
use std::thread;
#[allow(dead_code)]
mod support;
#[test]
fn basic_string_input() {
let (server, client) = support::new_one_server_one_client();
{
let mut client = client;
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain; charset=utf8\r\nContent-Length: 5\r\n\r\nhello")).unwrap();
}
let mut request = server.recv().unwrap();
let mut output = String::new();
request.as_reader().read_to_string(&mut output).unwrap();
assert_eq!(output, "hello");
}
#[test]
fn wrong_content_length() {
let (server, client) = support::new_one_server_one_client();
{
let mut client = client;
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain; charset=utf8\r\nContent-Length: 3\r\n\r\nhello")).unwrap();
}
let mut request = server.recv().unwrap();
let mut output = String::new();
request.as_reader().read_to_string(&mut output).unwrap();
assert_eq!(output, "hel");
}
#[test]
fn expect_100_continue() {
let (server, client) = support::new_one_server_one_client();
let mut client = client;
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nExpect: 100-continue\r\nContent-Type: text/plain; charset=utf8\r\nContent-Length: 5\r\n\r\n")).unwrap();
client.flush().unwrap();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let mut request = server.recv().unwrap();
let mut output = String::new();
request.as_reader().read_to_string(&mut output).unwrap();
assert_eq!(output, "hello");
tx.send(()).unwrap();
});
// client.set_keepalive(Some(3)).unwrap(); FIXME: reenable this
let mut content = vec![0; 12];
client.read(&mut content).unwrap();
assert!(&content[9..].starts_with(b"100")); // 100 status code
(write!(client, "hello")).unwrap();
client.flush().unwrap();
client.shutdown(Shutdown::Write).unwrap();
rx.recv().unwrap();
}
#[test]
fn unsupported_expect_header() {
let mut client = support::new_client_to_hello_world_server();
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nExpect: 189-dummy\r\nContent-Type: text/plain; charset=utf8\r\n\r\n")).unwrap();
// client.set_keepalive(Some(3)).unwrap(); FIXME: reenable this
let mut content = String::new();
client.read_to_string(&mut content).unwrap();
assert!(&content[9..].starts_with("417")); // 417 status code
}
#[test]
fn invalid_header_name() {
let mut client = support::new_client_to_hello_world_server();
// note the space hidden in the Content-Length, which is invalid
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Type: text/plain; charset=utf8\r\nContent-Length : 5\r\n\r\nhello")).unwrap();
let mut content = String::new();
client.read_to_string(&mut content).unwrap();
assert!(&content[9..].starts_with("400 Bad Request")); // 400 status code
}