lightstreamer_rs/utils/
proxy.rs1#[derive(Debug, Clone, PartialEq, Eq)]
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 #[must_use]
33 pub fn new(
34 proxy_type: ProxyType,
35 host: String,
36 port: u16,
37 user: Option<String>,
38 password: Option<String>,
39 ) -> Self {
40 Proxy {
41 proxy_type,
42 host,
43 port,
44 user,
45 password,
46 }
47 }
48
49 #[inline]
51 #[must_use]
52 pub fn get_proxy_type(&self) -> &ProxyType {
53 &self.proxy_type
54 }
55
56 #[inline]
58 #[must_use]
59 pub fn get_host(&self) -> &str {
60 &self.host
61 }
62
63 #[inline]
65 #[must_use]
66 pub fn get_port(&self) -> u16 {
67 self.port
68 }
69
70 #[inline]
72 #[must_use]
73 pub fn get_user(&self) -> Option<&String> {
74 self.user.as_ref()
75 }
76
77 #[inline]
79 #[must_use]
80 pub fn get_password(&self) -> Option<&String> {
81 self.password.as_ref()
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87#[repr(u8)]
88pub enum ProxyType {
89 Http,
91 Socks4,
93 Socks5,
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 #[test]
102 fn test_proxy_creation() {
103 let proxy = Proxy::new(
104 ProxyType::Http,
105 "proxy.example.com".to_string(),
106 8080,
107 Some("username".to_string()),
108 Some("password".to_string()),
109 );
110
111 assert_eq!(*proxy.get_proxy_type(), ProxyType::Http);
112 assert_eq!(proxy.get_host(), "proxy.example.com");
113 assert_eq!(proxy.get_port(), 8080);
114 assert_eq!(proxy.get_user(), Some(&"username".to_string()));
115 assert_eq!(proxy.get_password(), Some(&"password".to_string()));
116 }
117
118 #[test]
119 fn test_proxy_creation_without_credentials() {
120 let proxy = Proxy::new(
121 ProxyType::Socks5,
122 "proxy.example.com".to_string(),
123 1080,
124 None,
125 None,
126 );
127
128 assert_eq!(*proxy.get_proxy_type(), ProxyType::Socks5);
129 assert_eq!(proxy.get_host(), "proxy.example.com");
130 assert_eq!(proxy.get_port(), 1080);
131 assert_eq!(proxy.get_user(), None);
132 assert_eq!(proxy.get_password(), None);
133 }
134
135 #[test]
136 fn test_proxy_creation_with_username_only() {
137 let proxy = Proxy::new(
138 ProxyType::Socks4,
139 "proxy.example.com".to_string(),
140 1080,
141 Some("username".to_string()),
142 None,
143 );
144
145 assert_eq!(*proxy.get_proxy_type(), ProxyType::Socks4);
146 assert_eq!(proxy.get_host(), "proxy.example.com");
147 assert_eq!(proxy.get_port(), 1080);
148 assert_eq!(proxy.get_user(), Some(&"username".to_string()));
149 assert_eq!(proxy.get_password(), None);
150 }
151
152 #[test]
153 fn test_proxy_type_equality() {
154 assert_eq!(ProxyType::Http, ProxyType::Http);
155 assert_eq!(ProxyType::Socks4, ProxyType::Socks4);
156 assert_eq!(ProxyType::Socks5, ProxyType::Socks5);
157 assert_ne!(ProxyType::Http, ProxyType::Socks4);
158 assert_ne!(ProxyType::Http, ProxyType::Socks5);
159 assert_ne!(ProxyType::Socks4, ProxyType::Socks5);
160 }
161
162 #[test]
163 fn test_proxy_debug_format() {
164 let proxy = Proxy::new(
165 ProxyType::Http,
166 "proxy.example.com".to_string(),
167 8080,
168 Some("username".to_string()),
169 Some("password".to_string()),
170 );
171
172 let debug_string = format!("{:?}", proxy);
173
174 assert!(debug_string.contains("Http"));
175 assert!(debug_string.contains("proxy.example.com"));
176 assert!(debug_string.contains("8080"));
177 assert!(debug_string.contains("username"));
178 assert!(debug_string.contains("password"));
179 }
180
181 #[test]
182 fn test_proxy_type_debug_format() {
183 assert_eq!(format!("{:?}", ProxyType::Http), "Http");
184 assert_eq!(format!("{:?}", ProxyType::Socks4), "Socks4");
185 assert_eq!(format!("{:?}", ProxyType::Socks5), "Socks5");
186 }
187
188 #[test]
189 fn test_proxy_with_ipv4_address() {
190 let proxy = Proxy::new(ProxyType::Http, "192.168.1.1".to_string(), 8080, None, None);
192
193 assert_eq!(proxy.get_host(), "192.168.1.1");
194 }
195
196 #[test]
197 fn test_proxy_with_ipv6_address() {
198 let proxy = Proxy::new(
200 ProxyType::Http,
201 "2001:0db8:85a3:0000:0000:8a2e:0370:7334".to_string(),
202 8080,
203 None,
204 None,
205 );
206
207 assert_eq!(proxy.get_host(), "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
208 }
209}