Skip to main content

rust_serv/vhost/
config.rs

1//! Virtual host configuration
2
3use std::path::PathBuf;
4
5/// Virtual host configuration
6#[derive(Debug, Clone, PartialEq)]
7pub struct VHostConfig {
8    /// Host name (domain)
9    pub host: String,
10    /// Root directory for this host
11    pub root: PathBuf,
12    /// Enable directory indexing
13    pub enable_indexing: bool,
14    /// Enable compression
15    pub enable_compression: bool,
16    /// Custom index files
17    pub index_files: Vec<String>,
18}
19
20impl VHostConfig {
21    /// Create a new virtual host config
22    pub fn new(host: impl Into<String>, root: impl Into<PathBuf>) -> Self {
23        Self {
24            host: host.into(),
25            root: root.into(),
26            enable_indexing: true,
27            enable_compression: true,
28            index_files: vec!["index.html".to_string(), "index.htm".to_string()],
29        }
30    }
31
32    /// Set enable indexing
33    pub fn with_indexing(mut self, enabled: bool) -> Self {
34        self.enable_indexing = enabled;
35        self
36    }
37
38    /// Set enable compression
39    pub fn with_compression(mut self, enabled: bool) -> Self {
40        self.enable_compression = enabled;
41        self
42    }
43
44    /// Set index files
45    pub fn with_index_files(mut self, files: Vec<String>) -> Self {
46        self.index_files = files;
47        self
48    }
49
50    /// Check if this host matches the given hostname
51    pub fn matches(&self, hostname: &str) -> bool {
52        self.host.eq_ignore_ascii_case(hostname)
53    }
54}
55
56impl Default for VHostConfig {
57    fn default() -> Self {
58        Self {
59            host: "localhost".to_string(),
60            root: PathBuf::from("."),
61            enable_indexing: true,
62            enable_compression: true,
63            index_files: vec!["index.html".to_string(), "index.htm".to_string()],
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_vhost_config_creation() {
74        let config = VHostConfig::new("example.com", "/var/www/example");
75        assert_eq!(config.host, "example.com");
76        assert_eq!(config.root, PathBuf::from("/var/www/example"));
77    }
78
79    #[test]
80    fn test_vhost_config_with_indexing() {
81        let config = VHostConfig::new("example.com", "/var/www")
82            .with_indexing(false);
83        assert!(!config.enable_indexing);
84    }
85
86    #[test]
87    fn test_vhost_config_with_compression() {
88        let config = VHostConfig::new("example.com", "/var/www")
89            .with_compression(false);
90        assert!(!config.enable_compression);
91    }
92
93    #[test]
94    fn test_vhost_config_with_index_files() {
95        let config = VHostConfig::new("example.com", "/var/www")
96            .with_index_files(vec!["default.html".to_string()]);
97        assert_eq!(config.index_files, vec!["default.html"]);
98    }
99
100    #[test]
101    fn test_vhost_config_matches() {
102        let config = VHostConfig::new("example.com", "/var/www");
103        assert!(config.matches("example.com"));
104        assert!(config.matches("EXAMPLE.COM"));
105        assert!(!config.matches("other.com"));
106    }
107
108    #[test]
109    fn test_vhost_config_default() {
110        let config = VHostConfig::default();
111        assert_eq!(config.host, "localhost");
112        assert_eq!(config.root, PathBuf::from("."));
113    }
114
115    #[test]
116    fn test_vhost_config_clone() {
117        let config = VHostConfig::new("example.com", "/var/www");
118        let cloned = config.clone();
119        assert_eq!(config, cloned);
120    }
121
122    #[test]
123    fn test_vhost_config_default_index_files() {
124        let config = VHostConfig::default();
125        assert!(config.index_files.contains(&"index.html".to_string()));
126        assert!(config.index_files.contains(&"index.htm".to_string()));
127    }
128}