lightstreamer_rs/utils/
proxy.rs

1/// Simple class representing a Proxy configuration.
2///
3/// An instance of this class can be used through `ConnectionOptions.setProxy()` to instruct
4/// a `LightstreamerClient` to connect to the Lightstreamer Server passing through a proxy.
5///
6/// # Parameters
7///
8/// * `proxy_type`: the proxy type
9/// * `host`: the proxy host
10/// * `port`: the proxy port
11/// * `user`: the user name to be used to validate against the proxy. Optional.
12/// * `password`: the password to be used to validate against the proxy. Optional.
13#[derive(Debug)]
14pub struct Proxy {
15    proxy_type: ProxyType,
16    host: String,
17    port: u16,
18    user: Option<String>,
19    password: Option<String>,
20}
21
22impl Proxy {
23    /// Creates a new instance of `Proxy`.
24    ///
25    /// # Parameters
26    ///
27    /// * `proxy_type`: the proxy type
28    /// * `host`: the proxy host
29    /// * `port`: the proxy port
30    /// * `user`: the user name to be used to validate against the proxy. Optional.
31    /// * `password`: the password to be used to validate against the proxy. Optional.
32    pub fn new(
33        proxy_type: ProxyType,
34        host: String,
35        port: u16,
36        user: Option<String>,
37        password: Option<String>,
38    ) -> Self {
39        Proxy {
40            proxy_type,
41            host,
42            port,
43            user,
44            password,
45        }
46    }
47
48    /// Returns the proxy type.
49    pub fn get_proxy_type(&self) -> &ProxyType {
50        &self.proxy_type
51    }
52
53    /// Returns the proxy host.
54    pub fn get_host(&self) -> &str {
55        &self.host
56    }
57
58    /// Returns the proxy port.
59    pub fn get_port(&self) -> u16 {
60        self.port
61    }
62
63    /// Returns the proxy user name.
64    pub fn get_user(&self) -> Option<&String> {
65        self.user.as_ref()
66    }
67
68    /// Returns the proxy password.
69    pub fn get_password(&self) -> Option<&String> {
70        self.password.as_ref()
71    }
72}
73
74/// Represents the type of proxy.
75#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub enum ProxyType {
77    /// HTTP proxy.
78    Http,
79    /// SOCKS4 proxy.
80    Socks4,
81    /// SOCKS5 proxy.
82    Socks5,
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_proxy_creation() {
91        let proxy = Proxy::new(
92            ProxyType::Http,
93            "proxy.example.com".to_string(),
94            8080,
95            Some("username".to_string()),
96            Some("password".to_string()),
97        );
98
99        assert_eq!(*proxy.get_proxy_type(), ProxyType::Http);
100        assert_eq!(proxy.get_host(), "proxy.example.com");
101        assert_eq!(proxy.get_port(), 8080);
102        assert_eq!(proxy.get_user().unwrap(), "username");
103        assert_eq!(proxy.get_password().unwrap(), "password");
104    }
105
106    #[test]
107    fn test_proxy_creation_without_credentials() {
108        let proxy = Proxy::new(
109            ProxyType::Socks5,
110            "proxy.example.com".to_string(),
111            1080,
112            None,
113            None,
114        );
115
116        assert_eq!(*proxy.get_proxy_type(), ProxyType::Socks5);
117        assert_eq!(proxy.get_host(), "proxy.example.com");
118        assert_eq!(proxy.get_port(), 1080);
119        assert_eq!(proxy.get_user(), None);
120        assert_eq!(proxy.get_password(), None);
121    }
122
123    #[test]
124    fn test_proxy_creation_with_username_only() {
125        let proxy = Proxy::new(
126            ProxyType::Socks4,
127            "proxy.example.com".to_string(),
128            1080,
129            Some("username".to_string()),
130            None,
131        );
132
133        assert_eq!(*proxy.get_proxy_type(), ProxyType::Socks4);
134        assert_eq!(proxy.get_host(), "proxy.example.com");
135        assert_eq!(proxy.get_port(), 1080);
136        assert_eq!(proxy.get_user().unwrap(), "username");
137        assert_eq!(proxy.get_password(), None);
138    }
139
140    #[test]
141    fn test_proxy_type_equality() {
142        assert_eq!(ProxyType::Http, ProxyType::Http);
143        assert_eq!(ProxyType::Socks4, ProxyType::Socks4);
144        assert_eq!(ProxyType::Socks5, ProxyType::Socks5);
145        assert_ne!(ProxyType::Http, ProxyType::Socks4);
146        assert_ne!(ProxyType::Http, ProxyType::Socks5);
147        assert_ne!(ProxyType::Socks4, ProxyType::Socks5);
148    }
149
150    #[test]
151    fn test_proxy_debug_format() {
152        let proxy = Proxy::new(
153            ProxyType::Http,
154            "proxy.example.com".to_string(),
155            8080,
156            Some("username".to_string()),
157            Some("password".to_string()),
158        );
159
160        let debug_string = format!("{:?}", proxy);
161
162        assert!(debug_string.contains("Http"));
163        assert!(debug_string.contains("proxy.example.com"));
164        assert!(debug_string.contains("8080"));
165        assert!(debug_string.contains("username"));
166        assert!(debug_string.contains("password"));
167    }
168
169    #[test]
170    fn test_proxy_type_debug_format() {
171        assert_eq!(format!("{:?}", ProxyType::Http), "Http");
172        assert_eq!(format!("{:?}", ProxyType::Socks4), "Socks4");
173        assert_eq!(format!("{:?}", ProxyType::Socks5), "Socks5");
174    }
175
176    #[test]
177    fn test_proxy_with_ipv4_address() {
178        // Crear un proxy con dirección IPv4
179        let proxy = Proxy::new(ProxyType::Http, "192.168.1.1".to_string(), 8080, None, None);
180
181        assert_eq!(proxy.get_host(), "192.168.1.1");
182    }
183
184    #[test]
185    fn test_proxy_with_ipv6_address() {
186        // Crear un proxy con dirección IPv6
187        let proxy = Proxy::new(
188            ProxyType::Http,
189            "2001:0db8:85a3:0000:0000:8a2e:0370:7334".to_string(),
190            8080,
191            None,
192            None,
193        );
194
195        assert_eq!(proxy.get_host(), "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
196    }
197}