1 extern crate tiny_http;
2 
3 use std::io::{Read, Write};
4 use std::net::Shutdown;
5 use std::sync::mpsc;
6 use std::thread;
7 
8 #[allow(dead_code)]
9 mod support;
10 
11 #[test]
basic_string_input()12 fn basic_string_input() {
13     let (server, client) = support::new_one_server_one_client();
14 
15     {
16         let mut client = client;
17         (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();
18     }
19 
20     let mut request = server.recv().unwrap();
21 
22     let mut output = String::new();
23     request.as_reader().read_to_string(&mut output).unwrap();
24     assert_eq!(output, "hello");
25 }
26 
27 #[test]
wrong_content_length()28 fn wrong_content_length() {
29     let (server, client) = support::new_one_server_one_client();
30 
31     {
32         let mut client = client;
33         (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();
34     }
35 
36     let mut request = server.recv().unwrap();
37 
38     let mut output = String::new();
39     request.as_reader().read_to_string(&mut output).unwrap();
40     assert_eq!(output, "hel");
41 }
42 
43 #[test]
expect_100_continue()44 fn expect_100_continue() {
45     let (server, client) = support::new_one_server_one_client();
46 
47     let mut client = client;
48     (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();
49     client.flush().unwrap();
50 
51     let (tx, rx) = mpsc::channel();
52 
53     thread::spawn(move || {
54         let mut request = server.recv().unwrap();
55         let mut output = String::new();
56         request.as_reader().read_to_string(&mut output).unwrap();
57         assert_eq!(output, "hello");
58         tx.send(()).unwrap();
59     });
60 
61     // client.set_keepalive(Some(3)).unwrap(); FIXME: reenable this
62     let mut content = vec![0; 12];
63     client.read(&mut content).unwrap();
64     assert!(&content[9..].starts_with(b"100")); // 100 status code
65 
66     (write!(client, "hello")).unwrap();
67     client.flush().unwrap();
68     client.shutdown(Shutdown::Write).unwrap();
69 
70     rx.recv().unwrap();
71 }
72 
73 #[test]
unsupported_expect_header()74 fn unsupported_expect_header() {
75     let mut client = support::new_client_to_hello_world_server();
76 
77     (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();
78 
79     // client.set_keepalive(Some(3)).unwrap(); FIXME: reenable this
80     let mut content = String::new();
81     client.read_to_string(&mut content).unwrap();
82     assert!(&content[9..].starts_with("417")); // 417 status code
83 }
84 
85 #[test]
invalid_header_name()86 fn invalid_header_name() {
87     let mut client = support::new_client_to_hello_world_server();
88 
89     // note the space hidden in the Content-Length, which is invalid
90     (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();
91 
92     let mut content = String::new();
93     client.read_to_string(&mut content).unwrap();
94     assert!(&content[9..].starts_with("400 Bad Request")); // 400 status code
95 }
96 
97 #[test]
custom_content_type_response_header()98 fn custom_content_type_response_header() {
99     let (server, mut stream) = support::new_one_server_one_client();
100     write!(
101         stream,
102         "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
103     )
104     .unwrap();
105 
106     let request = server.recv().unwrap();
107     request
108         .respond(
109             tiny_http::Response::from_string("{\"custom\": \"Content-Type\"}").with_header(
110                 "Content-Type: application/json"
111                     .parse::<tiny_http::Header>()
112                     .unwrap(),
113             ),
114         )
115         .unwrap();
116 
117     let mut content = String::new();
118     stream.read_to_string(&mut content).unwrap();
119 
120     assert!(content.ends_with("{\"custom\": \"Content-Type\"}"));
121     assert_ne!(content.find("Content-Type: application/json"), None);
122 }
123