Skip to main content

rust_serv/vhost/
host.rs

1//! Virtual host representation
2
3use std::path::PathBuf;
4
5/// Virtual host
6#[derive(Debug, Clone)]
7pub struct VirtualHost {
8    /// Host configuration
9    pub config: super::config::VHostConfig,
10    /// Whether this is the default host
11    pub is_default: bool,
12}
13
14impl VirtualHost {
15    /// Create a new virtual host
16    pub fn new(config: super::config::VHostConfig) -> Self {
17        Self {
18            config,
19            is_default: false,
20        }
21    }
22
23    /// Create a default virtual host
24    pub fn default_host(root: impl Into<PathBuf>) -> Self {
25        let config = super::config::VHostConfig::new("_default_", root);
26        Self {
27            config,
28            is_default: true,
29        }
30    }
31
32    /// Set as default host
33    pub fn set_default(&mut self, is_default: bool) {
34        self.is_default = is_default;
35    }
36
37    /// Check if this host matches the given hostname
38    pub fn matches(&self, hostname: &str) -> bool {
39        if self.is_default {
40            return true;
41        }
42        self.config.matches(hostname)
43    }
44
45    /// Get root directory
46    pub fn root(&self) -> &PathBuf {
47        &self.config.root
48    }
49
50    /// Get host name
51    pub fn host(&self) -> &str {
52        &self.config.host
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use super::super::config::VHostConfig;
60
61    #[test]
62    fn test_virtual_host_creation() {
63        let config = VHostConfig::new("example.com", "/var/www");
64        let vhost = VirtualHost::new(config);
65        
66        assert_eq!(vhost.host(), "example.com");
67        assert_eq!(vhost.root(), &PathBuf::from("/var/www"));
68        assert!(!vhost.is_default);
69    }
70
71    #[test]
72    fn test_virtual_host_default() {
73        let vhost = VirtualHost::default_host("/var/default");
74        
75        assert!(vhost.is_default);
76        assert_eq!(vhost.host(), "_default_");
77    }
78
79    #[test]
80    fn test_virtual_host_set_default() {
81        let config = VHostConfig::new("example.com", "/var/www");
82        let mut vhost = VirtualHost::new(config);
83        
84        vhost.set_default(true);
85        assert!(vhost.is_default);
86        
87        vhost.set_default(false);
88        assert!(!vhost.is_default);
89    }
90
91    #[test]
92    fn test_virtual_host_matches() {
93        let config = VHostConfig::new("example.com", "/var/www");
94        let vhost = VirtualHost::new(config);
95        
96        assert!(vhost.matches("example.com"));
97        assert!(vhost.matches("EXAMPLE.COM"));
98        assert!(!vhost.matches("other.com"));
99    }
100
101    #[test]
102    fn test_virtual_host_default_matches_all() {
103        let vhost = VirtualHost::default_host("/var/default");
104        
105        // Default host should match everything
106        assert!(vhost.matches("example.com"));
107        assert!(vhost.matches("anything.org"));
108        assert!(vhost.matches("localhost"));
109    }
110
111    #[test]
112    fn test_virtual_host_root() {
113        let config = VHostConfig::new("example.com", "/var/www/example");
114        let vhost = VirtualHost::new(config);
115        
116        assert_eq!(vhost.root(), &PathBuf::from("/var/www/example"));
117    }
118
119    #[test]
120    fn test_virtual_host_clone() {
121        let config = VHostConfig::new("example.com", "/var/www");
122        let vhost = VirtualHost::new(config);
123        let cloned = vhost.clone();
124        
125        assert_eq!(vhost.host(), cloned.host());
126        assert_eq!(vhost.root(), cloned.root());
127    }
128}