| use serde::{Deserialize, Serialize}; |
| use strum_macros::EnumString; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, fake::Dummy)] |
| #[serde(rename_all = "camelCase")] |
| pub enum TlsVersion { |
| #[serde(rename = "1.0")] |
| V1_0, |
| #[serde(rename = "1.1")] |
| V1_1, |
| #[serde(rename = "1.2")] |
| V1_2, |
| #[default] |
| #[serde(rename = "1.3")] |
| V1_3, |
| } |
|
|
| impl std::fmt::Display for TlsVersion { |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| match self { |
| TlsVersion::V1_0 => write!(f, "1.0"), |
| TlsVersion::V1_1 => write!(f, "1.1"), |
| TlsVersion::V1_2 => write!(f, "1.2"), |
| TlsVersion::V1_3 => write!(f, "1.3"), |
| } |
| } |
| } |
|
|
| impl std::str::FromStr for TlsVersion { |
| type Err = String; |
|
|
| fn from_str(s: &str) -> Result<Self, Self::Err> { |
| match s { |
| "1.0" => Ok(TlsVersion::V1_0), |
| "1.1" => Ok(TlsVersion::V1_1), |
| "1.2" => Ok(TlsVersion::V1_2), |
| "1.3" => Ok(TlsVersion::V1_3), |
| _ => Err(format!( |
| "Invalid TLS version: {s}. Valid options are: 1.0, 1.1, 1.2, 1.3" |
| )), |
| } |
| } |
| } |
|
|
| #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, EnumString, fake::Dummy)] |
| #[serde(rename_all = "camelCase")] |
| #[strum(serialize_all = "lowercase")] |
| pub enum TlsBackend { |
| #[default] |
| Default, |
| Rustls, |
| } |
|
|
| impl std::fmt::Display for TlsBackend { |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| match self { |
| TlsBackend::Default => write!(f, "default"), |
| TlsBackend::Rustls => write!(f, "rustls"), |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, fake::Dummy)] |
| #[serde(rename_all = "camelCase")] |
| pub struct HttpConfig { |
| pub connect_timeout: u64, |
| pub read_timeout: u64, |
| pub pool_idle_timeout: u64, |
| pub pool_max_idle_per_host: usize, |
| pub max_redirects: usize, |
| pub hickory: bool, |
| pub tls_backend: TlsBackend, |
| |
| |
| pub min_tls_version: Option<TlsVersion>, |
| |
| |
| pub max_tls_version: Option<TlsVersion>, |
| |
| pub adaptive_window: bool, |
| |
| |
| pub keep_alive_interval: Option<u64>, |
| |
| pub keep_alive_timeout: u64, |
| |
| pub keep_alive_while_idle: bool, |
| |
| pub accept_invalid_certs: bool, |
| |
| |
| pub root_cert_paths: Option<Vec<String>>, |
| } |
|
|
| impl Default for HttpConfig { |
| fn default() -> Self { |
| Self { |
| connect_timeout: 30, |
| read_timeout: 900, |
| |
| pool_idle_timeout: 90, |
| pool_max_idle_per_host: 5, |
| max_redirects: 10, |
| hickory: false, |
| tls_backend: TlsBackend::default(), |
| min_tls_version: None, |
| max_tls_version: None, |
| |
| adaptive_window: true, |
| keep_alive_interval: Some(60), |
| keep_alive_timeout: 10, |
| keep_alive_while_idle: true, |
| accept_invalid_certs: false, |
| root_cert_paths: None, |
| } |
| } |
| } |
|
|
| #[cfg(test)] |
| mod tests { |
| use std::str::FromStr; |
|
|
| use super::*; |
|
|
| #[test] |
| fn test_tls_version_from_str() { |
| assert_eq!(TlsVersion::from_str("1.0").unwrap(), TlsVersion::V1_0); |
| assert_eq!(TlsVersion::from_str("1.1").unwrap(), TlsVersion::V1_1); |
| assert_eq!(TlsVersion::from_str("1.2").unwrap(), TlsVersion::V1_2); |
| assert_eq!(TlsVersion::from_str("1.3").unwrap(), TlsVersion::V1_3); |
|
|
| assert!(TlsVersion::from_str("invalid").is_err()); |
| assert!(TlsVersion::from_str("2.0").is_err()); |
| } |
|
|
| #[test] |
| fn test_tls_version_display() { |
| assert_eq!(TlsVersion::V1_0.to_string(), "1.0"); |
| assert_eq!(TlsVersion::V1_1.to_string(), "1.1"); |
| assert_eq!(TlsVersion::V1_2.to_string(), "1.2"); |
| assert_eq!(TlsVersion::V1_3.to_string(), "1.3"); |
| } |
|
|
| #[test] |
| fn test_tls_version_default() { |
| assert_eq!(TlsVersion::default(), TlsVersion::V1_3); |
| } |
|
|
| #[test] |
| fn test_http_config_with_tls_versions() { |
| let config = HttpConfig { |
| min_tls_version: Some(TlsVersion::V1_2), |
| max_tls_version: Some(TlsVersion::V1_3), |
| ..HttpConfig::default() |
| }; |
|
|
| assert_eq!(config.min_tls_version, Some(TlsVersion::V1_2)); |
| assert_eq!(config.max_tls_version, Some(TlsVersion::V1_3)); |
| } |
|
|
| #[test] |
| fn test_http_config_http2_defaults() { |
| let config = HttpConfig::default(); |
|
|
| assert!(config.adaptive_window); |
| assert_eq!(config.keep_alive_interval, Some(60)); |
| assert_eq!(config.keep_alive_timeout, 10); |
| assert!(config.keep_alive_while_idle); |
| } |
|
|
| #[test] |
| fn test_http_config_http2_custom_values() { |
| let config = HttpConfig { |
| adaptive_window: false, |
| keep_alive_interval: None, |
| keep_alive_timeout: 30, |
| keep_alive_while_idle: false, |
| ..HttpConfig::default() |
| }; |
|
|
| assert!(!config.adaptive_window); |
| assert_eq!(config.keep_alive_interval, None); |
| assert_eq!(config.keep_alive_timeout, 30); |
| assert!(!config.keep_alive_while_idle); |
| } |
|
|
| #[test] |
| fn test_http_config_accept_invalid_certs_defaults() { |
| let config = HttpConfig::default(); |
| assert!(!config.accept_invalid_certs); |
| } |
|
|
| #[test] |
| fn test_http_config_accept_invalid_certs_custom() { |
| let config = HttpConfig { accept_invalid_certs: true, ..HttpConfig::default() }; |
| assert!(config.accept_invalid_certs); |
| } |
|
|
| #[test] |
| fn test_http_config_root_cert_paths_custom() { |
| let cert_paths = vec![ |
| "/path/to/cert1.pem".to_string(), |
| "/path/to/cert2.crt".to_string(), |
| ]; |
| let config = HttpConfig { |
| root_cert_paths: Some(cert_paths.clone()), |
| ..HttpConfig::default() |
| }; |
| assert_eq!(config.root_cert_paths, Some(cert_paths)); |
| } |
| } |
|
|