Skip to main content

rust_serv/error_pages/
handler.rs

1//! Error page handler
2
3use super::templates::ErrorTemplates;
4
5/// Handler for generating error responses
6#[derive(Debug, Clone)]
7pub struct ErrorPageHandler {
8    templates: ErrorTemplates,
9}
10
11impl ErrorPageHandler {
12    /// Create a new error page handler
13    pub fn new() -> Self {
14        Self {
15            templates: ErrorTemplates::new(),
16        }
17    }
18
19    /// Create with custom templates
20    pub fn with_templates(templates: ErrorTemplates) -> Self {
21        Self { templates }
22    }
23
24    /// Get mutable templates
25    pub fn templates_mut(&mut self) -> &mut ErrorTemplates {
26        &mut self.templates
27    }
28
29    /// Get templates reference
30    pub fn templates(&self) -> &ErrorTemplates {
31        &self.templates
32    }
33
34    /// Generate error page HTML
35    pub fn render(&self, status_code: u16) -> String {
36        self.templates
37            .get(status_code)
38            .map(|t| t.content)
39            .unwrap_or_else(|| format!("Error {}", status_code))
40    }
41
42    /// Check if custom template exists
43    pub fn has_custom(&self, status_code: u16) -> bool {
44        self.templates.has_custom(status_code)
45    }
46}
47
48impl Default for ErrorPageHandler {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_handler_creation() {
60        let handler = ErrorPageHandler::new();
61        assert!(!handler.has_custom(404));
62    }
63
64    #[test]
65    fn test_handler_with_templates() {
66        let mut templates = ErrorTemplates::new();
67        templates.set_template(404, super::super::templates::ErrorTemplate::new(404, "Custom"));
68        
69        let handler = ErrorPageHandler::with_templates(templates);
70        assert!(handler.has_custom(404));
71    }
72
73    #[test]
74    fn test_handler_render_default() {
75        let handler = ErrorPageHandler::new();
76        let html = handler.render(404);
77        
78        assert!(html.contains("404"));
79        assert!(html.contains("<!DOCTYPE html>"));
80    }
81
82    #[test]
83    fn test_handler_render_custom() {
84        let mut templates = ErrorTemplates::new();
85        templates.set_template(404, super::super::templates::ErrorTemplate::new(404, "<html>Custom 404</html>"));
86        
87        let handler = ErrorPageHandler::with_templates(templates);
88        let html = handler.render(404);
89        
90        assert!(html.contains("Custom 404"));
91    }
92
93    #[test]
94    fn test_handler_templates_access() {
95        let handler = ErrorPageHandler::new();
96        assert_eq!(handler.templates().custom_count(), 0);
97    }
98
99    #[test]
100    fn test_handler_templates_mut_access() {
101        let mut handler = ErrorPageHandler::new();
102        handler.templates_mut().set_template(500, super::super::templates::ErrorTemplate::new(500, "test"));
103        
104        assert!(handler.has_custom(500));
105    }
106
107    #[test]
108    fn test_handler_default() {
109        let handler = ErrorPageHandler::default();
110        let html = handler.render(500);
111        assert!(html.contains("500"));
112    }
113}