Skip to content

Commit b058ad4

Browse files
committed
[test] the static-file-server example
See #3263
1 parent cd2d5e1 commit b058ad4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/static-file-server/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ tower = { version = "0.5.2", features = ["util"] }
1111
tower-http = { version = "0.6.1", features = ["fs", "trace"] }
1212
tracing = "0.1"
1313
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
14+
15+
16+
[dev-dependencies]
17+
http-body-util = "0.1.0"

examples/static-file-server/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//! cargo run -p example-static-file-server
55
//! ```
66
7+
#[cfg(test)]
8+
mod tests;
9+
710
use axum::{
811
extract::Request, handler::HandlerWithoutStateExt, http::StatusCode, routing::get, Router,
912
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use super::*;
2+
use axum::http::StatusCode as SC;
3+
use axum::{body::Body, http::Request};
4+
use http_body_util::BodyExt;
5+
use tower::ServiceExt;
6+
7+
const INDEX_HTML: &str = include_str!("../assets/index.html");
8+
const SCRIPT_JS: &str = include_str!("../assets/script.js");
9+
10+
const JS: &str = "text/javascript";
11+
const HTML: &str = "text/html";
12+
13+
async fn get_page(app: Router, path: &str) -> (StatusCode, String, String) {
14+
let response = app
15+
.oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
16+
.await
17+
.unwrap();
18+
19+
let status = response.status();
20+
let content_type = match response.headers().get("content-type") {
21+
Some(content_type) => content_type.to_str().unwrap().to_owned(),
22+
None => String::new(),
23+
};
24+
25+
let body = response.into_body();
26+
let bytes = body.collect().await.unwrap().to_bytes();
27+
let html = String::from_utf8(bytes.to_vec()).unwrap();
28+
29+
(status, content_type, html)
30+
}
31+
32+
async fn check(app: Router, path: &str, status: StatusCode, content_type: &str, content: &str) {
33+
let (actual_status, actual_content_type, actual_content) = get_page(app, path).await;
34+
assert_eq!(status, actual_status);
35+
assert_eq!(content_type, actual_content_type);
36+
assert_eq!(content, actual_content);
37+
}
38+
39+
#[tokio::test]
40+
async fn test_using_serve_dir() {
41+
let app = using_serve_dir;
42+
check(app(), "/assets/index.html", SC::OK, HTML, INDEX_HTML).await;
43+
check(app(), "/assets/script.js", SC::OK, JS, SCRIPT_JS).await;
44+
check(app(), "/assets/", SC::OK, HTML, INDEX_HTML).await;
45+
46+
check(app(), "/assets/other.html", SC::NOT_FOUND, "", "").await;
47+
}
48+
49+
#[tokio::test]
50+
async fn test_using_serve_dir_with_assets_fallback() {
51+
let app = using_serve_dir_with_assets_fallback;
52+
check(app(), "/assets/index.html", SC::OK, HTML, INDEX_HTML).await;
53+
check(app(), "/assets/script.js", SC::OK, JS, SCRIPT_JS).await;
54+
check(app(), "/assets/", SC::OK, HTML, INDEX_HTML).await;
55+
56+
check(
57+
app(),
58+
"/foo",
59+
SC::OK,
60+
"text/plain; charset=utf-8",
61+
"Hi from /foo",
62+
)
63+
.await;
64+
}

0 commit comments

Comments
 (0)