elif_http/
tests.rs

1//! Tests for HTTP server functionality
2
3#[cfg(test)]
4mod tests {
5    use super::*;
6    use crate::{HttpConfig, Server};
7    use elif_core::{
8        Container,
9        container::test_implementations::*,
10        app_config::{AppConfigTrait},
11    };
12    use std::sync::Arc;
13
14    fn create_test_container() -> Arc<Container> {
15        let config = Arc::new(create_test_config());
16        let database = Arc::new(TestDatabase::new()) as Arc<dyn elif_core::DatabaseConnection>;
17        
18        Container::builder()
19            .config(config)
20            .database(database)
21            .build()
22            .unwrap()
23            .into()
24    }
25
26    #[test]
27    fn test_http_config_defaults() {
28        let config = HttpConfig::default();
29        
30        assert_eq!(config.request_timeout_secs, 30);
31        assert_eq!(config.keep_alive_timeout_secs, 75);
32        assert_eq!(config.max_request_size, 16 * 1024 * 1024);
33        assert!(config.enable_tracing);
34        assert_eq!(config.health_check_path, "/health");
35        assert_eq!(config.shutdown_timeout_secs, 10);
36    }
37
38    #[test]
39    fn test_http_config_from_env() {
40        std::env::remove_var("HTTP_REQUEST_TIMEOUT");
41        std::env::remove_var("HTTP_KEEP_ALIVE_TIMEOUT");
42        
43        let config = HttpConfig::from_env().unwrap();
44        config.validate().unwrap();
45        
46        // Test duration helpers
47        assert_eq!(config.request_timeout().as_secs(), 30);
48        assert_eq!(config.keep_alive_timeout().as_secs(), 75);
49        assert_eq!(config.shutdown_timeout().as_secs(), 10);
50    }
51
52    #[test]
53    fn test_http_config_validation() {
54        let mut config = HttpConfig::default();
55        assert!(config.validate().is_ok());
56
57        // Test invalid request timeout
58        config.request_timeout_secs = 0;
59        assert!(config.validate().is_err());
60
61        // Test invalid health check path
62        config = HttpConfig::default();
63        config.health_check_path = "no-slash".to_string();
64        assert!(config.validate().is_err());
65
66        config.health_check_path = "".to_string();
67        assert!(config.validate().is_err());
68    }
69
70    #[test]
71    fn test_server_creation_with_container() {
72        let container = create_test_container();
73        let http_config = HttpConfig::default();
74
75        let server = Server::with_container(container, http_config);
76        assert!(server.is_ok());
77    }
78
79    #[test]
80    fn test_server_with_invalid_address() {
81        let container = create_test_container();
82        let http_config = HttpConfig::default();
83        
84        let server = Server::with_container(container, http_config).unwrap();
85        
86        // Test with invalid address - this should fail during listen, not creation
87        // For now, just verify server can be created with valid configuration
88        // Invalid address testing would be done in integration tests with actual listen() calls
89    }
90
91    #[tokio::test]
92    async fn test_health_check_endpoint() {
93        use crate::server::health_check;
94        
95        let container = create_test_container();
96        let config = HttpConfig::default();
97        
98        let response = health_check(container, config).await;
99        let value = response.0;
100        
101        assert_eq!(value["status"], "healthy");
102        assert_eq!(value["framework"], "Elif.rs");
103        assert!(value["timestamp"].is_number());
104    }
105
106    #[test]
107    fn test_http_error_types() {
108        use crate::error::HttpError;
109        use axum::http::StatusCode;
110        
111        let startup_error = HttpError::startup("Failed to bind");
112        assert_eq!(startup_error.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
113        assert_eq!(startup_error.error_code(), "SERVER_STARTUP_FAILED");
114        
115        let timeout_error = HttpError::RequestTimeout;
116        assert_eq!(timeout_error.status_code(), StatusCode::REQUEST_TIMEOUT);
117        assert_eq!(timeout_error.error_code(), "REQUEST_TIMEOUT");
118        
119        let bad_request = HttpError::bad_request("Invalid input");
120        assert_eq!(bad_request.status_code(), StatusCode::BAD_REQUEST);
121        assert_eq!(bad_request.error_code(), "BAD_REQUEST");
122    }
123
124    #[test]
125    fn test_error_conversions() {
126        use crate::error::HttpError;
127        use elif_core::app_config::ConfigError;
128        
129        // Test ConfigError conversion
130        let config_error = ConfigError::MissingEnvVar {
131            var: "TEST_VAR".to_string(),
132        };
133        let http_error: HttpError = config_error.into();
134        assert!(matches!(http_error, HttpError::ConfigError { .. }));
135        
136        // Test IO error conversion
137        let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "Access denied");
138        let http_error: HttpError = io_error.into();
139        assert!(matches!(http_error, HttpError::InternalError { .. }));
140    }
141}