Skip to content

Commit e9ff1c1

Browse files
committed
use content-type and mime
1 parent ed896d7 commit e9ff1c1

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ tracing = "0.1"
1313
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1414

1515
[dev-dependencies]
16+
headers = "0.4.0"
1617
http-body-util = "0.1.0"
18+
mime = "0.3.17"

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

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
use super::*;
22
use axum::http::StatusCode;
33
use axum::{body::Body, http::Request};
4+
use headers::ContentType;
45
use http_body_util::BodyExt;
56
use tower::ServiceExt;
67

78
const INDEX_HTML: &str = include_str!("../assets/index.html");
89
const SCRIPT_JS: &str = include_str!("../assets/script.js");
910

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) {
11+
async fn get_page(app: Router, path: &str) -> (StatusCode, Option<ContentType>, String) {
1412
let response = app
1513
.oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
1614
.await
1715
.unwrap();
1816

1917
let status = response.status();
2018
let content_type = match response.headers().get("content-type") {
21-
Some(content_type) => content_type.to_str().unwrap().to_owned(),
22-
None => String::new(),
19+
Some(header) => Some(header.to_str().unwrap().parse::<ContentType>().unwrap()),
20+
None => None,
2321
};
2422

2523
let body = response.into_body();
@@ -29,7 +27,13 @@ async fn get_page(app: Router, path: &str) -> (StatusCode, String, String) {
2927
(status, content_type, html)
3028
}
3129

32-
async fn check(app: Router, path: &str, status: StatusCode, content_type: &str, content: &str) {
30+
async fn check(
31+
app: Router,
32+
path: &str,
33+
status: StatusCode,
34+
content_type: Option<ContentType>,
35+
content: &str,
36+
) {
3337
let (actual_status, actual_content_type, actual_content) = get_page(app, path).await;
3438
assert_eq!(status, actual_status);
3539
assert_eq!(content_type, actual_content_type);
@@ -43,14 +47,27 @@ async fn test_using_serve_dir() {
4347
app(),
4448
"/assets/index.html",
4549
StatusCode::OK,
46-
HTML,
50+
Some(ContentType::html()),
4751
INDEX_HTML,
4852
)
4953
.await;
50-
check(app(), "/assets/script.js", StatusCode::OK, JS, SCRIPT_JS).await;
51-
check(app(), "/assets/", StatusCode::OK, HTML, INDEX_HTML).await;
52-
53-
check(app(), "/assets/other.html", StatusCode::NOT_FOUND, "", "").await;
54+
check(
55+
app(),
56+
"/assets/script.js",
57+
StatusCode::OK,
58+
Some(ContentType::from(mime::TEXT_JAVASCRIPT)),
59+
SCRIPT_JS,
60+
)
61+
.await;
62+
check(
63+
app(),
64+
"/assets/",
65+
StatusCode::OK,
66+
Some(ContentType::html()),
67+
INDEX_HTML,
68+
)
69+
.await;
70+
check(app(), "/assets/other.html", StatusCode::NOT_FOUND, None, "").await;
5471
}
5572

5673
#[tokio::test]
@@ -60,18 +77,32 @@ async fn test_using_serve_dir_with_assets_fallback() {
6077
app(),
6178
"/assets/index.html",
6279
StatusCode::OK,
63-
HTML,
80+
Some(ContentType::html()),
81+
INDEX_HTML,
82+
)
83+
.await;
84+
check(
85+
app(),
86+
"/assets/script.js",
87+
StatusCode::OK,
88+
Some(ContentType::from(mime::TEXT_JAVASCRIPT)),
89+
SCRIPT_JS,
90+
)
91+
.await;
92+
check(
93+
app(),
94+
"/assets/",
95+
StatusCode::OK,
96+
Some(ContentType::html()),
6497
INDEX_HTML,
6598
)
6699
.await;
67-
check(app(), "/assets/script.js", StatusCode::OK, JS, SCRIPT_JS).await;
68-
check(app(), "/assets/", StatusCode::OK, HTML, INDEX_HTML).await;
69100

70101
check(
71102
app(),
72103
"/foo",
73104
StatusCode::OK,
74-
"text/plain; charset=utf-8",
105+
Some(ContentType::text_utf8()),
75106
"Hi from /foo",
76107
)
77108
.await;

0 commit comments

Comments
 (0)