-
Notifications
You must be signed in to change notification settings - Fork 4
/
integration_test.rs
154 lines (117 loc) · 4.58 KB
/
integration_test.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
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
extern crate http_test_server;
use std::io::prelude::*;
use std::io::BufReader;
use std::net::TcpStream;
use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use http_test_server::TestServer;
use http_test_server::http::{Method, Status};
#[test]
fn test_defaults() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/defaults");
let requests = server.requests();
let response = request(server.port(), "/defaults", "GET");
let request_data = requests.recv().unwrap();
assert_eq!(response, "HTTP/1.1 200 Ok\r\n\r\n");
assert_eq!(request_data.url, "/defaults");
assert_eq!(request_data.method, "GET");
assert_eq!(request_data.headers, HashMap::new());
assert_eq!(resource.request_count(), 1);
}
#[test]
fn test_post_request() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/create");
resource
.method(Method::POST)
.status(Status::Created)
.header("Content-Type", "text")
.body("Everything is fine!");
let response = request(server.port(), "/create", "POST");
assert_eq!(response, "HTTP/1.1 201 Created\r\nContent-Type: text\r\n\r\nEverything is fine!");
}
#[test]
fn test_stream() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/stream");
resource
.stream()
.header("Content-Type", "text/event-stream")
.body(": initial data\n");
let resource_clone = resource.clone();
thread::spawn(move || {
thread::sleep(Duration::from_millis(100));
resource_clone.send_line("Hello.");
resource_clone.send_line("Is there anybody in there?");
resource_clone.send_line("Just nod if you can hear me.");
resource_clone.close_open_connections();
});
let stream = open_stream(server.port(), "/stream", "GET");
thread::sleep(Duration::from_millis(200));
let mut reader = BufReader::new(stream);
let mut response = String::new();
reader.read_to_string(&mut response).unwrap();
assert_eq!(response, "HTTP/1.1 200 Ok\r\nContent-Type: text/event-stream\r\n\r\n: initial data\nHello.\nIs there anybody in there?\nJust nod if you can hear me.\n");
}
#[test]
fn test_request_with_path_and_query_params() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/user/{userId}?filter=*&version=1");
resource
.status(Status::OK)
.header("Content-Type", "application/json")
.body(r#"{"id": 123, "userId": "{path.userId}", "filter": "{query.filter}", "v": {query.version}}"#);
let response = request(server.port(), "/user/superUser?filter=all&version=1", "GET");
assert_eq!(
response,
"HTTP/1.1 200 Ok\r\nContent-Type: application/json\r\n\r\n{\"id\": 123, \"userId\": \"superUser\", \"filter\": \"all\", \"v\": 1}"
);
}
#[test]
fn test_request_to_regex_uri() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/hello/[0-9]/[A-z]/.*");
let requests = server.requests();
let response = request(server.port(), "/hello/2/b/goodbye", "GET");
let request_data = requests.recv().unwrap();
assert_eq!(response, "HTTP/1.1 200 Ok\r\n\r\n");
assert_eq!(request_data.url, "/hello/2/b/goodbye");
assert_eq!(request_data.method, "GET");
assert_eq!(request_data.headers, HashMap::new());
assert_eq!(resource.request_count(), 1);
}
#[test]
fn request_to_loopback_ip() {
let server = TestServer::new().unwrap();
let resource = server.create_resource("/hello");
let host = format!("127.0.0.1:{}", server.port());
let mut stream = TcpStream::connect(host).unwrap();
stream.write("GET /hello HTTP/1.1\r\n\r\n".as_bytes()).unwrap();
stream.flush().unwrap();
let mut reader = BufReader::new(stream);
let mut response = String::new();
reader.read_to_string(&mut response).unwrap();
assert_eq!(response, "HTTP/1.1 200 Ok\r\n\r\n");
assert_eq!(resource.request_count(), 1);
}
fn request(port: u16, uri: &str, method: &str) -> String {
let stream = open_stream(port, uri, method);
let mut reader = BufReader::new(stream);
let mut response = String::new();
reader.read_to_string(&mut response).unwrap();
response
}
fn open_stream(port: u16, uri: &str, method: &str) -> TcpStream {
let host = format!("localhost:{}", port);
let mut stream = TcpStream::connect(host).unwrap();
let request = format!(
"{} {} HTTP/1.1\r\n\r\n",
method,
uri
);
stream.write(request.as_bytes()).unwrap();
stream.flush().unwrap();
stream
}