/
mod.rs
71 lines (58 loc) · 2.73 KB
/
mod.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
//! serve middleware
mod dir;
mod fs;
pub use dir::{Options, StaticDir};
pub use fs::StaticFile;
#[cfg(test)]
mod tests {
use salvo_core::hyper;
use salvo_core::prelude::*;
use super::*;
#[derive(Debug, Serialize, Deserialize)]
struct JwtClaims {
user: String,
exp: i64,
}
#[tokio::test]
async fn test_serve_static_files() {
let router = Router::with_path("<**path>").get(StaticDir::width_options(
vec!["../examples/static/test"],
Options {
dot_files: false,
listing: true,
defaults: vec!["index.html".to_owned()],
},
));
let service = Service::new(router);
async fn access(service: &Service, accept: &str, url: &str) -> String {
let req: Request = hyper::Request::builder()
.method("GET")
.header("accept", accept)
.uri(url)
.body(hyper::Body::empty())
.unwrap()
.into();
service.handle(req).await.take_text().await.unwrap()
}
let content = access(&service, "text/plain", "http://127.0.0.1:7979/").await;
assert!(content.contains("test1.txt") && content.contains("test2.txt"));
let content = access(&service, "text/xml", "http://127.0.0.1:7979/").await;
assert!(content.starts_with("<list>") && content.contains("test1.txt") && content.contains("test2.txt"));
let content = access(&service, "text/html", "http://127.0.0.1:7979/").await;
assert!(content.contains("<html>") && content.contains("test1.txt") && content.contains("test2.txt"));
let content = access(&service, "application/json", "http://127.0.0.1:7979/").await;
assert!(content.starts_with("{") && content.contains("test1.txt") && content.contains("test2.txt"));
let content = access(&service, "text/plain", "http://127.0.0.1:7979/test1.txt").await;
assert!(content.contains("copy1"));
let content = access(&service, "text/plain", "http://127.0.0.1:7979/test3.txt").await;
assert!(content.contains("Not Found"));
let content = access(&service, "text/plain", "http://127.0.0.1:7979/../girl/love/eat.txt").await;
assert!(content.contains("Not Found"));
let content = access(&service, "text/plain", "http://127.0.0.1:7979/dir1/test3.txt").await;
assert!(content.contains("copy3"));
let content = access(&service, "text/plain", "http://127.0.0.1:7979/dir1/dir2/test3.txt").await;
assert!(content == "dir2 test3");
let content = access(&service, "text/plain", "http://127.0.0.1:7979/dir1/../dir1/test3.txt").await;
assert!(content == "copy3");
}
}