Skip to main content

orion_accessor/addr/
proxy.rs

1use crate::prelude::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
5pub enum ProxyType {
6    Http,
7    Socks5,
8}
9
10#[derive(Clone, Debug, Getters, Serialize, Deserialize, PartialEq, Eq)]
11#[getset(get = "pub")]
12pub struct ProxyConfig {
13    url: String,
14}
15
16impl ProxyConfig {
17    pub fn new<S: Into<String>>(url: S) -> Self {
18        Self { url: url.into() }
19    }
20}
21
22impl EnvEvalable<ProxyConfig> for ProxyConfig {
23    fn env_eval(self, dict: &EnvDict) -> ProxyConfig {
24        Self {
25            url: self.url.env_eval(dict),
26        }
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use std::collections::HashMap;
34
35    #[test]
36    fn test_proxy_type_variants_and_functionality() {
37        let http = ProxyType::Http;
38        let socks5 = ProxyType::Socks5;
39
40        assert_eq!(http, http);
41        assert_eq!(socks5, socks5);
42        assert_ne!(http, socks5);
43
44        let yaml = serde_yaml::to_string(&http).unwrap();
45        let deserialized: ProxyType = serde_yaml::from_str(&yaml).unwrap();
46        assert_eq!(http, deserialized);
47    }
48
49    #[test]
50    fn test_proxy_config_creation_and_accessors() {
51        let config = ProxyConfig::new("http://proxy.example.com:8080");
52        assert_eq!(config.url(), "http://proxy.example.com:8080");
53
54        let cloned = config.clone();
55        assert_eq!(config, cloned);
56        assert_ne!(std::ptr::addr_of!(config), std::ptr::addr_of!(cloned));
57    }
58
59    #[test]
60    fn test_proxy_config_serialization_roundtrip() {
61        let original = ProxyConfig::new("socks5://proxy.example.com:1080");
62        let yaml = serde_yaml::to_string(&original).unwrap();
63        let deserialized: ProxyConfig = serde_yaml::from_str(&yaml).unwrap();
64        assert_eq!(original, deserialized);
65    }
66
67    #[test]
68    fn test_proxy_config_environment_evaluation() {
69        let config = ProxyConfig::new("http://$PROXY_HOST:$PROXY_PORT");
70        let mut dict = HashMap::new();
71        dict.insert("PROXY_HOST".to_string(), "proxy.example.com".into());
72        dict.insert("PROXY_PORT".to_string(), "8080".into());
73
74        // Test that environment evaluation doesn't panic
75        let evaluated = config.env_eval(&dict.into());
76        assert!(!evaluated.url().is_empty());
77    }
78
79    #[test]
80    fn test_proxy_config_with_different_schemes() {
81        let urls = vec![
82            "http://proxy.example.com:8080",
83            "https://proxy.example.com:443",
84            "socks5://proxy.example.com:1080",
85            "http://user:pass@proxy.example.com:8080",
86        ];
87
88        for url in urls {
89            let config = ProxyConfig::new(url);
90            assert_eq!(config.url(), url);
91
92            let yaml = serde_yaml::to_string(&config).unwrap();
93            let deserialized: ProxyConfig = serde_yaml::from_str(&yaml).unwrap();
94            assert_eq!(config, deserialized);
95        }
96    }
97}