lightstreamer_rs/utils/
proxy.rs1#[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 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 pub fn get_proxy_type(&self) -> &ProxyType {
50 &self.proxy_type
51 }
52
53 pub fn get_host(&self) -> &str {
55 &self.host
56 }
57
58 pub fn get_port(&self) -> u16 {
60 self.port
61 }
62
63 pub fn get_user(&self) -> Option<&String> {
65 self.user.as_ref()
66 }
67
68 pub fn get_password(&self) -> Option<&String> {
70 self.password.as_ref()
71 }
72}
73
74#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub enum ProxyType {
77 Http,
79 Socks4,
81 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 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 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}