webserver_colin_ugo/
utils.rs

1use std::path::Path;
2
3/// Cette fonction renvoie le type de contenu d'un fichier en fonction de son extension.
4/// 
5/// # Arguments
6///
7/// * `file_path` - Une tranche de chaîne contenant le chemin vers le fichier.
8///
9/// # Retourne
10///
11/// * Une tranche de chaîne contenant le type de contenu du fichier.
12pub fn get_content_type(file_path: &str) -> &str {
13    match Path::new(file_path)
14        .extension()
15        .and_then(|ext| ext.to_str())
16    {
17        Some("html") => "text/html",
18        Some("css") => "text/css",
19        Some("js") => "application/javascript",
20        Some("jpg") => "image/jpeg",
21        Some("png") => "image/png",
22        Some("svg") => "image/svg+xml",
23        Some("woff") => "font/woff",
24        Some("woff2") => "font/woff2",
25        _ => "text/plain",
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_get_content_type() {
35        // Test pour les fichiers HTML
36        assert_eq!(get_content_type("test.html"), "text/html");
37        
38        // Test pour les fichiers CSS
39        assert_eq!(get_content_type("styles.css"), "text/css");
40        
41        // Test pour les fichiers JavaScript
42        assert_eq!(get_content_type("script.js"), "application/javascript");
43        
44        // Test pour les images
45        assert_eq!(get_content_type("image.jpg"), "image/jpeg");
46        assert_eq!(get_content_type("icon.png"), "image/png");
47        assert_eq!(get_content_type("diagram.svg"), "image/svg+xml");
48        
49        // Test pour les polices
50        assert_eq!(get_content_type("font.woff"), "font/woff");
51        assert_eq!(get_content_type("font.woff2"), "font/woff2");
52        
53        // Test pour les extensions inconnues
54        assert_eq!(get_content_type("unknown.xyz"), "text/plain");
55        
56        // Test pour des fichiers sans extension
57        assert_eq!(get_content_type("README"), "text/plain");
58    }
59}