diff options
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r-- | cli/file_fetcher.rs | 104 |
1 files changed, 97 insertions, 7 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index e865bf945..29fe04d8a 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -505,12 +505,16 @@ fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType { | "text/typescript" | "video/vnd.dlna.mpeg-tts" | "video/mp2t" - | "application/x-typescript" => msg::MediaType::TypeScript, + | "application/x-typescript" => { + map_js_like_extension(path, msg::MediaType::TypeScript) + } "application/javascript" | "text/javascript" | "application/ecmascript" | "text/ecmascript" - | "application/x-javascript" => msg::MediaType::JavaScript, + | "application/x-javascript" => { + map_js_like_extension(path, msg::MediaType::JavaScript) + } "application/json" | "text/json" => msg::MediaType::Json, "text/plain" => map_file_extension(path), _ => { @@ -523,6 +527,21 @@ fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType { } } +fn map_js_like_extension( + path: &Path, + default: msg::MediaType, +) -> msg::MediaType { + match path.extension() { + None => default, + Some(os_str) => match os_str.to_str() { + None => default, + Some("jsx") => msg::MediaType::JSX, + Some("tsx") => msg::MediaType::TSX, + Some(_) => default, + }, + } +} + fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> { let string = str::from_utf8(&bytes).unwrap(); if let Some(i) = string.find('\n') { @@ -1410,7 +1429,7 @@ mod tests { } #[test] - fn test_map_content_type() { + fn test_map_content_type_extension_only() { // Extension only assert_eq!( map_content_type(Path::new("foo/bar.ts"), None), @@ -1429,6 +1448,10 @@ mod tests { msg::MediaType::JavaScript ); assert_eq!( + map_content_type(Path::new("foo/bar.txt"), None), + msg::MediaType::Unknown + ); + assert_eq!( map_content_type(Path::new("foo/bar.jsx"), None), msg::MediaType::JSX ); @@ -1437,14 +1460,13 @@ mod tests { msg::MediaType::Json ); assert_eq!( - map_content_type(Path::new("foo/bar.txt"), None), - msg::MediaType::Unknown - ); - assert_eq!( map_content_type(Path::new("foo/bar"), None), msg::MediaType::Unknown ); + } + #[test] + fn test_map_content_type_media_type_with_no_extension() { // Media Type assert_eq!( map_content_type(Path::new("foo/bar"), Some("application/typescript")), @@ -1494,6 +1516,10 @@ mod tests { map_content_type(Path::new("foo/bar"), Some("text/json")), msg::MediaType::Json ); + } + + #[test] + fn test_map_file_extension_media_type_with_extension() { assert_eq!( map_content_type(Path::new("foo/bar.ts"), Some("text/plain")), msg::MediaType::TypeScript @@ -1502,6 +1528,70 @@ mod tests { map_content_type(Path::new("foo/bar.ts"), Some("foo/bar")), msg::MediaType::Unknown ); + assert_eq!( + map_content_type( + Path::new("foo/bar.tsx"), + Some("application/typescript") + ), + msg::MediaType::TSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.tsx"), + Some("application/javascript") + ), + msg::MediaType::TSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.tsx"), + Some("application/x-typescript") + ), + msg::MediaType::TSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.tsx"), + Some("video/vnd.dlna.mpeg-tts") + ), + msg::MediaType::TSX + ); + assert_eq!( + map_content_type(Path::new("foo/bar.tsx"), Some("video/mp2t")), + msg::MediaType::TSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.jsx"), + Some("application/javascript") + ), + msg::MediaType::JSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.jsx"), + Some("application/x-typescript") + ), + msg::MediaType::JSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.jsx"), + Some("application/ecmascript") + ), + msg::MediaType::JSX + ); + assert_eq!( + map_content_type(Path::new("foo/bar.jsx"), Some("text/ecmascript")), + msg::MediaType::JSX + ); + assert_eq!( + map_content_type( + Path::new("foo/bar.jsx"), + Some("application/x-javascript") + ), + msg::MediaType::JSX + ); } #[test] |