-
Notifications
You must be signed in to change notification settings - Fork 39
/
e2e.rs
312 lines (272 loc) · 10.6 KB
/
e2e.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#[cfg(test)]
mod test {
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::Instant;
use std::{env, io};
use reqwest::StatusCode;
// Default timeout when waiting for wws to be ready
static DEFAULT_MAX_TIMEOUT: u64 = 30;
fn get_wws_path() -> PathBuf {
let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let binary = if cfg!(target_os = "windows") {
"wws.exe"
} else {
"wws"
};
// Use release when it's available
let wws_path = if path.join("target/release").join(binary).exists() {
path.join("target/release").join(binary)
} else {
path.join("target/debug").join(binary)
};
println!("[E2E] Running wws from {}", wws_path.display());
wws_path
}
fn run(example_path: &str, max_timeout: u64) -> io::Result<Child> {
let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let example_path = path.join("examples").join(example_path);
let wws_path = get_wws_path();
// Install missing runtimes
println!("[E2E] Installing missing runtimes");
Command::new(&wws_path)
.current_dir(&example_path)
.args(["runtimes", "install"])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()?;
// Run the example
println!("[E2E] Running the service");
let mut child = Command::new(&wws_path)
.arg(&example_path)
.stdout(Stdio::piped())
.spawn()?;
let stdout = child.stdout.take().unwrap();
let reader = BufReader::new(stdout);
// Set a max timeout
let instant = Instant::now();
for line in reader.lines() {
let line = line.unwrap();
// Break when ready of after the timeout
if line.contains("Start serving requests") {
break;
} else if instant.elapsed().as_secs() >= max_timeout {
println!("Timeout waiting for wws to be ready");
break;
}
}
Ok(child)
}
fn request_body(url: &str) -> Result<String, reqwest::Error> {
reqwest::blocking::get(url)?.text()
}
fn request_status(url: &str) -> Result<StatusCode, reqwest::Error> {
Ok(reqwest::blocking::get(url)?.status())
}
// Check the examples/js-json works
fn run_end_to_end_test(example: &str, max_timeout: u64, url: &str, expected_text: &str) {
println!("[E2E] Running example: {example}");
let mut child = run(example, max_timeout).expect("Failed to execute command");
// sleep_for(waiting_seconds);
let body = match request_body(url) {
Ok(body) => body,
Err(err) => {
eprintln!("[E2E] Error getting the body from the request to {url}");
eprintln!("[E2E] Error: {}", err);
String::new()
}
};
println!("[E2E] Body content: {body}");
println!("[E2E] Stopping wws process [{}]", &child.id());
child.kill().expect("Error stopping wws");
// Test
assert!(
body.contains(expected_text),
"result \"{body}\" does not contain \"{expected_text}\""
);
}
/// Runs the given example and ensures it returns a 404 error.
fn run_not_found_test(example: &str, max_timeout: u64, urls: &[&str]) {
println!("[E2E] Running example (not found): {example}");
let mut codes = Vec::new();
let mut child = run(example, max_timeout).expect("Failed to execute command");
for url in urls {
let code = match request_status(url) {
Ok(code) => code,
Err(err) => {
eprintln!("[E2E] Error getting the StatusCode from the request to {url}");
eprintln!("[E2E] Error: {}", err);
// Make it fail
StatusCode::FOUND
}
};
let body = match request_body(url) {
Ok(body) => body,
Err(err) => {
eprintln!("[E2E] Error getting the body from the request to {url}");
eprintln!("[E2E] Error: {}", err);
String::new()
}
};
codes.push((url, code));
println!("[E2E] URL: {url} / Status code: {code} / Body: {body}");
}
println!("[E2E] Stopping wws process [{}]", &child.id());
child.kill().expect("Error stopping wws");
// Assert all of them at the same time to avoid leaving an instance running
for (_url, code) in codes {
// Test
assert!(matches!(code, StatusCode::NOT_FOUND));
}
}
#[test]
fn test_end_to_end() {
// Run them in a method to ensure they run sequentially
examples_end_to_end();
not_found_end_to_end();
}
// Use this approach to run tests sequentially
fn examples_end_to_end() {
// Allow configuring waiting times. It avoids having long waiting times
// in development, while making it configurable in the CI
let max_timeout = env::var("E2E_MAX_WAITING_TIME").map_or(DEFAULT_MAX_TIMEOUT, |str| {
str.parse::<u64>().ok().unwrap_or(DEFAULT_MAX_TIMEOUT)
});
let tests = [
(
"rust-basic",
"http://localhost:8080/rust-basic",
"This page was generated by a Wasm module built from Rust",
),
(
&format!("components{}rust-basic", std::path::MAIN_SEPARATOR_STR),
"http://localhost:8080/rust-basic",
"This page was generated by a Wasm module built from Rust",
),
("rust-kv", "http://localhost:8080/rust-kv", "Counter: 0"),
(
&format!("components{}rust-kv", std::path::MAIN_SEPARATOR_STR),
"http://localhost:8080/rust-kv",
"Counter: 0",
),
(
"rust-params",
"http://localhost:8080/thisisatest",
"thisisatest",
),
(
&format!("components{}rust-params", std::path::MAIN_SEPARATOR_STR),
"http://localhost:8080/thisisatest",
"thisisatest",
),
(
"js-basic",
"http://localhost:8080",
"This page was generated by a JavaScript file",
),
(
"js-async",
"http://localhost:8080",
"This page was generated by a JavaScript (async worker) file",
),
(
"js-modules",
"http://localhost:8080",
"This page was generated by a JavaScript (module) file",
),
(
&format!("js-hono{}dist", std::path::MAIN_SEPARATOR_STR),
"http://localhost:8080",
"Hello from Hono",
),
(
"js-json",
"http://localhost:8080/handler",
"This message comes from an environment variable",
),
(
"js-params",
"http://localhost:8080/thisisatest",
"thisisatest",
),
(
"js-params",
"http://localhost:8080/main.css",
"This is just a comment for testing purposes",
),
(
"python-basic",
"http://localhost:8080/",
"This page was generated by a Python script",
),
(
"python-mount",
"http://localhost:8080/",
"This page was loaded from a mounted file",
),
(
"ruby-basic",
"http://localhost:8080/",
"This page was generated by a Ruby script",
),
];
for (example, url, expected_text) in tests {
run_end_to_end_test(example, max_timeout, url, expected_text);
}
}
fn not_found_end_to_end() {
// Allow configuring waiting times. It avoids having long waiting times
// in development, while making it configurable in the CI
let max_timeout = env::var("E2E_MAX_WAITING_TIME").map_or(DEFAULT_MAX_TIMEOUT, |str| {
str.parse::<u64>().ok().unwrap_or(DEFAULT_MAX_TIMEOUT)
});
let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let cargo_toml = path.join("Cargo.toml");
let cargo_toml_str = if cfg!(target_os = "windows") {
// Actix doesn't allow : and \ characters
// Remove the first two characters (like X:) and the \.
let no_root_path = cargo_toml
.to_string_lossy()
.chars()
.skip(3)
.collect::<String>();
format!("http://localhost:8080/{}", no_root_path).replace('\\', "/")
} else {
format!("http://localhost:8080{}", cargo_toml.to_string_lossy())
};
// Test we're not exposing wrong files
let tests = [
(
"js-basic",
Vec::from([
"http://localhost:8080/index.js",
"http://localhost:8080/examples/js-basic/index.js",
"http://localhost:8080/Cargo.toml",
// Check path traversal issues
"http://localhost:8080/../README.md",
"http://localhost:8080/%C0AE%C0AE%C0AFREADME.md",
"http://localhost:8080/%2e%2e/README.md",
cargo_toml_str.as_str(),
]),
),
(
"js-json",
Vec::from([
"http://localhost:8080/handler.js",
"http://localhost:8080/examples/js-json/handler.js",
"http://localhost:8080/Cargo.toml",
"http://localhost:8080/handler.toml",
// Check path traversal issues
"http://localhost:8080/../README.md",
"http://localhost:8080/%C0AE%C0AE%C0AFREADME.md",
"http://localhost:8080/%2e%2e/README.md",
cargo_toml_str.as_str(),
]),
),
];
for (example, urls) in tests {
run_not_found_test(example, max_timeout, &urls);
}
}
}