Skip to content

Commit b38d996

Browse files
committed
Test that static mime types are correct
1 parent 30ae906 commit b38d996

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/web/statics.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use iron::{
88
};
99
use mime_guess::MimeGuess;
1010
use router::Router;
11-
use std::{fs, path::Path};
11+
use std::{ffi::OsStr, fs, path::Path};
1212

1313
const STYLE_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
1414
const MENU_JS: &str = include_str!(concat!(env!("OUT_DIR"), "/menu.js"));
@@ -49,9 +49,22 @@ fn serve_file(req: &Request, file: &str) -> IronResult<Response> {
4949
let contents = ctry!(req, fs::read(&path));
5050

5151
// If we can detect the file's mime type, set it
52-
let content_type = MimeGuess::from_path(&path)
53-
.first()
54-
.map(|mime| ContentType(mime.as_ref().parse().unwrap()));
52+
// MimeGuess misses a lot of the file types we need, so there's a small wrapper
53+
// around it
54+
let content_type = path
55+
.extension()
56+
.and_then(OsStr::to_str)
57+
.and_then(|ext| match ext {
58+
"eot" => Some(ContentType(
59+
"application/vnd.ms-fontobject".parse().unwrap(),
60+
)),
61+
"woff2" => Some(ContentType("application/font-woff2".parse().unwrap())),
62+
"ttf" => Some(ContentType("application/x-font-ttf".parse().unwrap())),
63+
64+
_ => MimeGuess::from_path(&path)
65+
.first()
66+
.map(|mime| ContentType(mime.as_ref().parse().unwrap())),
67+
});
5568

5669
serve_resource(contents, content_type)
5770
}
@@ -200,10 +213,40 @@ mod tests {
200213
.send()?
201214
.status()
202215
.as_u16(),
203-
404
216+
404,
204217
);
205218

206219
Ok(())
207220
});
208221
}
222+
223+
#[test]
224+
fn static_mime_types() {
225+
wrapper(|env| {
226+
let web = env.frontend();
227+
228+
let files = &[
229+
("pure-min.css", "text/css"),
230+
("fa-brands-400.eot", "application/vnd.ms-fontobject"),
231+
("fa-brands-400.svg", "image/svg+xml"),
232+
("fa-brands-400.ttf", "application/x-font-ttf"),
233+
("fa-brands-400.woff", "application/font-woff"),
234+
("fa-brands-400.woff2", "application/font-woff2"),
235+
];
236+
237+
for (file, mime) in files {
238+
let url = format!("/-/static/{}", file);
239+
let resp = web.get(&url).send()?;
240+
241+
assert_eq!(
242+
resp.headers().get("Content-Type"),
243+
Some(&mime.parse().unwrap()),
244+
"{:?} has an incorrect content type",
245+
url,
246+
);
247+
}
248+
249+
Ok(())
250+
});
251+
}
209252
}

0 commit comments

Comments
 (0)