1 + | /*
|
2 + | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 + | * SPDX-License-Identifier: Apache-2.0
|
4 + | */
|
5 + |
|
6 + | //! Proxy configuration for HTTP clients
|
7 + | //!
|
8 + | //! This module provides types and utilities for configuring HTTP and HTTPS proxies,
|
9 + | //! including support for environment variable detection, authentication, and bypass rules.
|
10 + |
|
11 + | use http_1x::Uri;
|
12 + | use hyper_util::client::proxy::matcher::Matcher;
|
13 + | use std::fmt;
|
14 + |
|
15 + | /// Proxy configuration for HTTP clients
|
16 + | ///
|
17 + | /// Supports HTTP and HTTPS proxy configuration with authentication and bypass rules.
|
18 + | /// Can be configured programmatically or automatically detected from environment variables.
|
19 + | ///
|
20 + | /// # Examples
|
21 + | ///
|
22 + | /// ```rust
|
23 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
24 + | ///
|
25 + | /// // HTTP proxy for all traffic
|
26 + | /// let config = ProxyConfig::http("http://proxy.example.com:8080")?;
|
27 + | ///
|
28 + | /// // HTTPS traffic through HTTP proxy (common case - no TLS needed for proxy connection)
|
29 + | /// let config = ProxyConfig::https("http://proxy.example.com:8080")?
|
30 + | /// .with_basic_auth("username", "password")
|
31 + | /// .no_proxy("localhost,*.internal");
|
32 + | ///
|
33 + | /// // Detect from environment variables
|
34 + | /// let config = ProxyConfig::from_env();
|
35 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
36 + | /// ```
|
37 + | #[derive(Debug, Clone)]
|
38 + | pub struct ProxyConfig {
|
39 + | inner: ProxyConfigInner,
|
40 + | }
|
41 + |
|
42 + | /// Internal configuration representation
|
43 + | #[derive(Debug, Clone)]
|
44 + | enum ProxyConfigInner {
|
45 + | /// Use environment variable detection
|
46 + | FromEnvironment,
|
47 + | /// Explicit HTTP proxy
|
48 + | Http {
|
49 + | uri: Uri,
|
50 + | auth: Option<ProxyAuth>,
|
51 + | no_proxy: Option<String>,
|
52 + | },
|
53 + | /// Explicit HTTPS proxy
|
54 + | Https {
|
55 + | uri: Uri,
|
56 + | auth: Option<ProxyAuth>,
|
57 + | no_proxy: Option<String>,
|
58 + | },
|
59 + | /// Proxy for all traffic
|
60 + | All {
|
61 + | uri: Uri,
|
62 + | auth: Option<ProxyAuth>,
|
63 + | no_proxy: Option<String>,
|
64 + | },
|
65 + | /// Explicitly disabled
|
66 + | Disabled,
|
67 + | }
|
68 + |
|
69 + | /// Proxy authentication configuration
|
70 + | ///
|
71 + | /// Stored for later conversion to hyper-util format.
|
72 + | #[derive(Debug, Clone)]
|
73 + | struct ProxyAuth {
|
74 + | /// Username for authentication
|
75 + | username: String,
|
76 + | /// Password for authentication
|
77 + | password: String,
|
78 + | }
|
79 + |
|
80 + | /// Errors that can occur during proxy configuration
|
81 + | #[derive(Debug)]
|
82 + | pub enum ProxyError {
|
83 + | /// Invalid proxy URL
|
84 + | InvalidUrl(String),
|
85 + | /// Environment variable parsing error
|
86 + | EnvVarError(String),
|
87 + | }
|
88 + |
|
89 + | impl fmt::Display for ProxyError {
|
90 + | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
91 + | match self {
|
92 + | ProxyError::InvalidUrl(url) => write!(f, "invalid proxy URL: {}", url),
|
93 + | ProxyError::EnvVarError(msg) => write!(f, "environment variable error: {}", msg),
|
94 + | }
|
95 + | }
|
96 + | }
|
97 + |
|
98 + | impl std::error::Error for ProxyError {}
|
99 + |
|
100 + | impl ProxyConfig {
|
101 + | /// Create a new proxy configuration for HTTP traffic only
|
102 + | ///
|
103 + | /// # Arguments
|
104 + | /// * `proxy_url` - The HTTP proxy URL (e.g., "http://proxy.example.com:8080")
|
105 + | ///
|
106 + | /// # Examples
|
107 + | /// ```rust
|
108 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
109 + | ///
|
110 + | /// let config = ProxyConfig::http("http://proxy.example.com:8080")?;
|
111 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
112 + | /// ```
|
113 + | pub fn http<U>(proxy_url: U) -> Result<Self, ProxyError>
|
114 + | where
|
115 + | U: TryInto<Uri>,
|
116 + | U::Error: fmt::Display,
|
117 + | {
|
118 + | let uri = proxy_url
|
119 + | .try_into()
|
120 + | .map_err(|e| ProxyError::InvalidUrl(e.to_string()))?;
|
121 + |
|
122 + | Self::validate_proxy_uri(&uri)?;
|
123 + |
|
124 + | Ok(ProxyConfig {
|
125 + | inner: ProxyConfigInner::Http {
|
126 + | uri,
|
127 + | auth: None,
|
128 + | no_proxy: None,
|
129 + | },
|
130 + | })
|
131 + | }
|
132 + |
|
133 + | /// Create a new proxy configuration for HTTPS traffic only
|
134 + | ///
|
135 + | /// This proxy will only be used for `https://` requests. HTTP requests
|
136 + | /// will connect directly unless a separate HTTP proxy is configured.
|
137 + | ///
|
138 + | /// The proxy URL itself can use either HTTP or HTTPS scheme:
|
139 + | /// - `http://proxy.example.com:8080` - Connect to proxy using HTTP (no TLS needed)
|
140 + | /// - `https://proxy.example.com:8080` - Connect to proxy using HTTPS (TLS required)
|
141 + | ///
|
142 + | /// **Note**: If the proxy URL itself uses HTTPS scheme, TLS support must be
|
143 + | /// available when building the connector, otherwise connections will fail.
|
144 + | ///
|
145 + | /// # Arguments
|
146 + | /// * `proxy_url` - The proxy URL (e.g., "http://proxy.example.com:8080" or "https://proxy.example.com:8080")
|
147 + | ///
|
148 + | /// # Examples
|
149 + | /// ```rust
|
150 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
151 + | ///
|
152 + | /// // HTTPS traffic through HTTP proxy (no TLS needed for proxy connection)
|
153 + | /// let config = ProxyConfig::https("http://proxy.example.com:8080")?;
|
154 + | ///
|
155 + | /// // HTTPS traffic through HTTPS proxy (TLS needed for proxy connection)
|
156 + | /// let config = ProxyConfig::https("https://secure-proxy.example.com:8080")?;
|
157 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
158 + | /// ```
|
159 + | pub fn https<U>(proxy_url: U) -> Result<Self, ProxyError>
|
160 + | where
|
161 + | U: TryInto<Uri>,
|
162 + | U::Error: fmt::Display,
|
163 + | {
|
164 + | let uri = proxy_url
|
165 + | .try_into()
|
166 + | .map_err(|e| ProxyError::InvalidUrl(e.to_string()))?;
|
167 + |
|
168 + | Self::validate_proxy_uri(&uri)?;
|
169 + |
|
170 + | Ok(ProxyConfig {
|
171 + | inner: ProxyConfigInner::Https {
|
172 + | uri,
|
173 + | auth: None,
|
174 + | no_proxy: None,
|
175 + | },
|
176 + | })
|
177 + | }
|
178 + |
|
179 + | /// Create a new proxy configuration for all HTTP and HTTPS traffic
|
180 + | ///
|
181 + | /// This proxy will be used for both `http://` and `https://` requests.
|
182 + | /// This is equivalent to setting both HTTP and HTTPS proxies to the same URL.
|
183 + | ///
|
184 + | /// **Note**: If the proxy URL itself uses HTTPS scheme, TLS support must be
|
185 + | /// available when building the connector, otherwise connections will fail.
|
186 + | ///
|
187 + | /// # Arguments
|
188 + | /// * `proxy_url` - The proxy URL (e.g., "http://proxy.example.com:8080")
|
189 + | ///
|
190 + | /// # Examples
|
191 + | /// ```rust
|
192 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
193 + | ///
|
194 + | /// let config = ProxyConfig::all("http://proxy.example.com:8080")?;
|
195 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
196 + | /// ```
|
197 + | pub fn all<U>(proxy_url: U) -> Result<Self, ProxyError>
|
198 + | where
|
199 + | U: TryInto<Uri>,
|
200 + | U::Error: fmt::Display,
|
201 + | {
|
202 + | let uri = proxy_url
|
203 + | .try_into()
|
204 + | .map_err(|e| ProxyError::InvalidUrl(e.to_string()))?;
|
205 + |
|
206 + | Self::validate_proxy_uri(&uri)?;
|
207 + |
|
208 + | Ok(ProxyConfig {
|
209 + | inner: ProxyConfigInner::All {
|
210 + | uri,
|
211 + | auth: None,
|
212 + | no_proxy: None,
|
213 + | },
|
214 + | })
|
215 + | }
|
216 + |
|
217 + | /// Create a proxy configuration that disables all proxy usage
|
218 + | ///
|
219 + | /// This is useful for explicitly disabling proxy support even when
|
220 + | /// environment variables are set.
|
221 + | ///
|
222 + | /// # Examples
|
223 + | /// ```rust
|
224 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
225 + | ///
|
226 + | /// let config = ProxyConfig::disabled();
|
227 + | /// ```
|
228 + | pub fn disabled() -> Self {
|
229 + | ProxyConfig {
|
230 + | inner: ProxyConfigInner::Disabled,
|
231 + | }
|
232 + | }
|
233 + |
|
234 + | /// Add basic authentication to this proxy configuration
|
235 + | ///
|
236 + | /// # Arguments
|
237 + | /// * `username` - Username for proxy authentication
|
238 + | /// * `password` - Password for proxy authentication
|
239 + | ///
|
240 + | /// # Examples
|
241 + | /// ```rust
|
242 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
243 + | ///
|
244 + | /// let config = ProxyConfig::http("http://proxy.example.com:8080")?
|
245 + | /// .with_basic_auth("username", "password");
|
246 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
247 + | /// ```
|
248 + | pub fn with_basic_auth<U, P>(mut self, username: U, password: P) -> Self
|
249 + | where
|
250 + | U: Into<String>,
|
251 + | P: Into<String>,
|
252 + | {
|
253 + | let auth = ProxyAuth {
|
254 + | username: username.into(),
|
255 + | password: password.into(),
|
256 + | };
|
257 + |
|
258 + | match &mut self.inner {
|
259 + | ProxyConfigInner::Http {
|
260 + | auth: ref mut a, ..
|
261 + | } => *a = Some(auth),
|
262 + | ProxyConfigInner::Https {
|
263 + | auth: ref mut a, ..
|
264 + | } => *a = Some(auth),
|
265 + | ProxyConfigInner::All {
|
266 + | auth: ref mut a, ..
|
267 + | } => *a = Some(auth),
|
268 + | ProxyConfigInner::FromEnvironment | ProxyConfigInner::Disabled => {
|
269 + | // Cannot add auth to environment or disabled configs
|
270 + | }
|
271 + | }
|
272 + |
|
273 + | self
|
274 + | }
|
275 + |
|
276 + | /// Add NO_PROXY rules to this configuration
|
277 + | ///
|
278 + | /// NO_PROXY rules specify hosts that should bypass the proxy and connect directly.
|
279 + | ///
|
280 + | /// # Arguments
|
281 + | /// * `rules` - Comma-separated list of bypass rules
|
282 + | ///
|
283 + | /// # Examples
|
284 + | /// ```rust
|
285 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
286 + | ///
|
287 + | /// let config = ProxyConfig::http("http://proxy.example.com:8080")?
|
288 + | /// .no_proxy("localhost,127.0.0.1,*.internal,10.0.0.0/8");
|
289 + | /// # Ok::<(), Box<dyn std::error::Error>>(())
|
290 + | /// ```
|
291 + | pub fn no_proxy<S: AsRef<str>>(mut self, rules: S) -> Self {
|
292 + | let rules_str = rules.as_ref().to_string();
|
293 + |
|
294 + | match &mut self.inner {
|
295 + | ProxyConfigInner::Http {
|
296 + | no_proxy: ref mut n,
|
297 + | ..
|
298 + | } => *n = Some(rules_str),
|
299 + | ProxyConfigInner::Https {
|
300 + | no_proxy: ref mut n,
|
301 + | ..
|
302 + | } => *n = Some(rules_str),
|
303 + | ProxyConfigInner::All {
|
304 + | no_proxy: ref mut n,
|
305 + | ..
|
306 + | } => *n = Some(rules_str),
|
307 + | ProxyConfigInner::FromEnvironment | ProxyConfigInner::Disabled => {
|
308 + | // Cannot add no_proxy to environment or disabled configs
|
309 + | // Environment configs will use NO_PROXY env var
|
310 + | // FIXME - is this what we want?
|
311 + | }
|
312 + | }
|
313 + |
|
314 + | self
|
315 + | }
|
316 + |
|
317 + | /// Create proxy configuration from environment variables
|
318 + | ///
|
319 + | /// Reads standard proxy environment variables:
|
320 + | /// - `HTTP_PROXY` / `http_proxy`: HTTP proxy URL
|
321 + | /// - `HTTPS_PROXY` / `https_proxy`: HTTPS proxy URL
|
322 + | /// - `ALL_PROXY` / `all_proxy`: Proxy for all protocols (fallback)
|
323 + | /// - `NO_PROXY` / `no_proxy`: Comma-separated bypass rules
|
324 + | ///
|
325 + | /// If no proxy environment variables are set, this returns a configuration
|
326 + | /// that won't intercept any requests (equivalent to no proxy).
|
327 + | ///
|
328 + | /// # Examples
|
329 + | /// ```rust
|
330 + | /// use aws_smithy_http_client::proxy::ProxyConfig;
|
331 + | ///
|
332 + | /// // Always succeeds, even if no environment variables are set
|
333 + | /// let config = ProxyConfig::from_env();
|
334 + | /// ```
|
335 + | pub fn from_env() -> Self {
|
336 + | // Delegate to environment variable parsing
|
337 + | // If no env vars are set, creates a matcher that doesn't intercept anything
|
338 + | ProxyConfig {
|
339 + | inner: ProxyConfigInner::FromEnvironment,
|
340 + | }
|
341 + | }
|
342 + |
|
343 + | /// Check if proxy is disabled (no proxy configuration)
|
344 + | pub fn is_disabled(&self) -> bool {
|
345 + | matches!(self.inner, ProxyConfigInner::Disabled)
|
346 + | }
|
347 + |
|
348 + | /// Check if this configuration uses environment variables
|
349 + | pub fn is_from_env(&self) -> bool {
|
350 + | matches!(self.inner, ProxyConfigInner::FromEnvironment)
|
351 + | }
|
352 + |
|
353 + | /// Convert this configuration to internal proxy matcher
|
354 + | ///
|
355 + | /// This method converts the user-friendly configuration to the internal
|
356 + | /// proxy matching implementation used by the HTTP client.
|
357 + | pub(crate) fn into_hyper_util_matcher(self) -> Matcher {
|
358 + | match self.inner {
|
359 + | ProxyConfigInner::FromEnvironment => Matcher::from_env(),
|
360 + | ProxyConfigInner::Http {
|
361 + | uri,
|
362 + | auth,
|
363 + | no_proxy,
|
364 + | } => {
|
365 + | let mut builder = Matcher::builder();
|
366 + |
|
367 + | // Set HTTP proxy with authentication embedded in URL if present
|
368 + | let proxy_url = Self::build_proxy_url(uri, auth);
|
369 + | builder = builder.http(proxy_url);
|
370 + |
|
371 + | // Add NO_PROXY rules if present
|
372 + | if let Some(no_proxy_rules) = no_proxy {
|
373 + | builder = builder.no(no_proxy_rules);
|
374 + | }
|
375 + |
|
376 + | builder.build()
|
377 + | }
|
378 + | ProxyConfigInner::Https {
|
379 + | uri,
|
380 + | auth,
|
381 + | no_proxy,
|
382 + | } => {
|
383 + | let mut builder = Matcher::builder();
|
384 + |
|
385 + | // Set HTTPS proxy with authentication embedded in URL if present
|
386 + | let proxy_url = Self::build_proxy_url(uri, auth);
|
387 + | builder = builder.https(proxy_url);
|
388 + |
|
389 + | // Add NO_PROXY rules if present
|
390 + | if let Some(no_proxy_rules) = no_proxy {
|
391 + | builder = builder.no(no_proxy_rules);
|
392 + | }
|
393 + |
|
394 + | builder.build()
|
395 + | }
|
396 + | ProxyConfigInner::All {
|
397 + | uri,
|
398 + | auth,
|
399 + | no_proxy,
|
400 + | } => {
|
401 + | let mut builder = Matcher::builder();
|
402 + |
|
403 + | // Set proxy for all traffic with authentication embedded in URL if present
|
404 + | let proxy_url = Self::build_proxy_url(uri, auth);
|
405 + | builder = builder.all(proxy_url);
|
406 + |
|
407 + | // Add NO_PROXY rules if present
|
408 + | if let Some(no_proxy_rules) = no_proxy {
|
409 + | builder = builder.no(no_proxy_rules);
|
410 + | }
|
411 + |
|
412 + | builder.build()
|
413 + | }
|
414 + | ProxyConfigInner::Disabled => {
|
415 + | // Create an empty matcher that won't intercept anything
|
416 + | Matcher::builder().build()
|
417 + | }
|
418 + | }
|
419 + | }
|
420 + |
|
421 + | /// Check if this proxy configuration requires TLS support
|
422 + | ///
|
423 + | /// Returns true if any of the configured proxy URLs use HTTPS scheme,
|
424 + | /// which requires TLS to establish the connection to the proxy server.
|
425 + | pub(crate) fn requires_tls(&self) -> bool {
|
426 + | match &self.inner {
|
427 + | ProxyConfigInner::Http { uri, .. } => uri.scheme_str() == Some("https"),
|
428 + | ProxyConfigInner::Https { uri, .. } => uri.scheme_str() == Some("https"),
|
429 + | ProxyConfigInner::All { uri, .. } => uri.scheme_str() == Some("https"),
|
430 + | ProxyConfigInner::FromEnvironment => {
|
431 + | // Check environment variables for HTTPS proxy URLs
|
432 + | Self::env_vars_require_tls()
|
433 + | }
|
434 + | ProxyConfigInner::Disabled => false,
|
435 + | }
|
436 + | }
|
437 + |
|
438 + | /// Check if any environment proxy variables contain HTTPS URLs
|
439 + | fn env_vars_require_tls() -> bool {
|
440 + | let proxy_vars = [
|
441 + | "HTTP_PROXY",
|
442 + | "http_proxy",
|
443 + | "HTTPS_PROXY",
|
444 + | "https_proxy",
|
445 + | "ALL_PROXY",
|
446 + | "all_proxy",
|
447 + | ];
|
448 + |
|
449 + | for var in &proxy_vars {
|
450 + | if let Ok(proxy_url) = std::env::var(var) {
|
451 + | if !proxy_url.is_empty() {
|
452 + | // Simple check for https:// scheme
|
453 + | if proxy_url.starts_with("https://") {
|
454 + | return true;
|
455 + | }
|
456 + | }
|
457 + | }
|
458 + | }
|
459 + | false
|
460 + | }
|
461 + |
|
462 + | fn validate_proxy_uri(uri: &Uri) -> Result<(), ProxyError> {
|
463 + | // Validate scheme
|
464 + | match uri.scheme_str() {
|
465 + | Some("http") | Some("https") => {}
|
466 + | Some(scheme) => {
|
467 + | return Err(ProxyError::InvalidUrl(format!(
|
468 + | "unsupported proxy scheme: {}",
|
469 + | scheme
|
470 + | )));
|
471 + | }
|
472 + | None => {
|
473 + | return Err(ProxyError::InvalidUrl(
|
474 + | "proxy URL must include scheme (http:// or https://)".to_string(),
|
475 + | ));
|
476 + | }
|
477 + | }
|
478 + |
|
479 + | // Validate host
|
480 + | if uri.host().is_none() {
|
481 + | return Err(ProxyError::InvalidUrl(
|
482 + | "proxy URL must include host".to_string(),
|
483 + | ));
|
484 + | }
|
485 + |
|
486 + | Ok(())
|
487 + | }
|
488 + |
|
489 + | fn build_proxy_url(uri: Uri, auth: Option<ProxyAuth>) -> String {
|
490 + | let uri_str = uri.to_string();
|
491 + |
|
492 + | if let Some(auth) = auth {
|
493 + | // Embed authentication in the URL: scheme://username:password@host:port/path
|
494 + | if let Some(scheme_end) = uri_str.find("://") {
|
495 + | let scheme = &uri_str[..scheme_end + 3];
|
496 + | let rest = &uri_str[scheme_end + 3..];
|
497 + |
|
498 + | // Check if auth is already present in the URI
|
499 + | if rest.contains('@') {
|
500 + | // Auth already present, return as-is
|
501 + | uri_str
|
502 + | } else {
|
503 + | // Add auth to the URI
|
504 + | format!("{}{}:{}@{}", scheme, auth.username, auth.password, rest)
|
505 + | }
|
506 + | } else {
|
507 + | // Invalid URI format, return as-is
|
508 + | uri_str
|
509 + | }
|
510 + | } else {
|
511 + | // No authentication, return URI as-is
|
512 + | uri_str
|
513 + | }
|
514 + | }
|
515 + | }
|
516 + |
|
517 + | #[cfg(test)]
|
518 + | mod tests {
|
519 + | use super::*;
|
520 + | use std::env;
|
521 + |
|
522 + | #[test]
|
523 + | fn test_proxy_config_http() {
|
524 + | let config = ProxyConfig::http("http://proxy.example.com:8080").unwrap();
|
525 + | assert!(!config.is_disabled());
|
526 + | assert!(!config.is_from_env());
|
527 + | }
|
528 + |
|
529 + | #[test]
|
530 + | fn test_proxy_config_https() {
|
531 + | let config = ProxyConfig::https("http://proxy.example.com:8080").unwrap();
|
532 + | assert!(!config.is_disabled());
|
533 + | assert!(!config.is_from_env());
|
534 + | }
|
535 + |
|
536 + | #[test]
|
537 + | fn test_proxy_config_all() {
|
538 + | let config = ProxyConfig::all("http://proxy.example.com:8080").unwrap();
|
539 + | assert!(!config.is_disabled());
|
540 + | assert!(!config.is_from_env());
|
541 + | }
|
542 + |
|
543 + | #[test]
|
544 + | fn test_proxy_config_disabled() {
|
545 + | let config = ProxyConfig::disabled();
|
546 + | assert!(config.is_disabled());
|
547 + | assert!(!config.is_from_env());
|
548 + | }
|
549 + |
|
550 + | #[test]
|
551 + | fn test_proxy_config_with_auth() {
|
552 + | let config = ProxyConfig::http("http://proxy.example.com:8080")
|
553 + | .unwrap()
|
554 + | .with_basic_auth("user", "pass");
|
555 + |
|
556 + | // Auth is stored internally
|
557 + | assert!(!config.is_disabled());
|
558 + | }
|
559 + |
|
560 + | #[test]
|
561 + | fn test_proxy_config_with_no_proxy() {
|
562 + | let config = ProxyConfig::http("http://proxy.example.com:8080")
|
563 + | .unwrap()
|
564 + | .no_proxy("localhost,*.internal");
|
565 + |
|
566 + | // NO_PROXY rules are stored internally
|
567 + | assert!(!config.is_disabled());
|
568 + | }
|
569 + |
|
570 + | #[test]
|
571 + | fn test_proxy_config_invalid_url() {
|
572 + | let result = ProxyConfig::http("not-a-url");
|
573 + | assert!(result.is_err());
|
574 + | }
|
575 + |
|
576 + | #[test]
|
577 + | fn test_proxy_config_invalid_scheme() {
|
578 + | let result = ProxyConfig::http("ftp://proxy.example.com:8080");
|
579 + | assert!(result.is_err());
|
580 + | }
|
581 + |
|
582 + | #[test]
|
583 + | #[serial_test::serial]
|
584 + | fn test_proxy_config_from_env_with_vars() {
|
585 + | // Save original environment
|
586 + | let original_http = env::var("HTTP_PROXY");
|
587 + |
|
588 + | // Set test environment
|
589 + | env::set_var("HTTP_PROXY", "http://test-proxy:8080");
|
590 + |
|
591 + | let config = ProxyConfig::from_env();
|
592 + | assert!(config.is_from_env());
|
593 + |
|
594 + | // Restore original environment
|
595 + | match original_http {
|
596 + | Ok(val) => env::set_var("HTTP_PROXY", val),
|
597 + | Err(_) => env::remove_var("HTTP_PROXY"),
|
598 + | }
|
599 + | }
|
600 + |
|
601 + | #[test]
|
602 + | #[serial_test::serial]
|
603 + | fn test_proxy_config_from_env_without_vars() {
|
604 + | // Save original environment
|
605 + | let original_vars: Vec<_> = [
|
606 + | "HTTP_PROXY",
|
607 + | "http_proxy",
|
608 + | "HTTPS_PROXY",
|
609 + | "https_proxy",
|
610 + | "ALL_PROXY",
|
611 + | "all_proxy",
|
612 + | ]
|
613 + | .iter()
|
614 + | .map(|var| (*var, env::var(var)))
|
615 + | .collect();
|
616 + |
|
617 + | // Clear all proxy environment variables
|
618 + | for (var, _) in &original_vars {
|
619 + | env::remove_var(var);
|
620 + | }
|
621 + |
|
622 + | let config = ProxyConfig::from_env();
|
623 + | assert!(config.is_from_env());
|
624 + |
|
625 + | // Restore original environment
|
626 + | for (var, original_value) in original_vars {
|
627 + | match original_value {
|
628 + | Ok(val) => env::set_var(var, val),
|
629 + | Err(_) => env::remove_var(var),
|
630 + | }
|
631 + | }
|
632 + | }
|
633 + |
|
634 + | #[test]
|
635 + | #[serial_test::serial]
|
636 + | fn test_auth_cannot_be_added_to_env_config() {
|
637 + | // Save original environment
|
638 + | let original_http = env::var("HTTP_PROXY");
|
639 + | env::set_var("HTTP_PROXY", "http://test-proxy:8080");
|
640 + |
|
641 + | let config = ProxyConfig::from_env().with_basic_auth("user", "pass"); // This should be ignored
|
642 + |
|
643 + | assert!(config.is_from_env());
|
644 + |
|
645 + | // Restore original environment
|
646 + | match original_http {
|
647 + | Ok(val) => env::set_var("HTTP_PROXY", val),
|
648 + | Err(_) => env::remove_var("HTTP_PROXY"),
|
649 + | }
|
650 + | }
|
651 + |
|
652 + | #[test]
|
653 + | #[serial_test::serial]
|
654 + | fn test_no_proxy_cannot_be_added_to_env_config() {
|
655 + | // Save original environment
|
656 + | let original_http = env::var("HTTP_PROXY");
|
657 + | env::set_var("HTTP_PROXY", "http://test-proxy:8080");
|
658 + |
|
659 + | let config = ProxyConfig::from_env().no_proxy("localhost"); // This should be ignored
|
660 + |
|
661 + | assert!(config.is_from_env());
|
662 + |
|
663 + | // Restore original environment
|
664 + | match original_http {
|
665 + | Ok(val) => env::set_var("HTTP_PROXY", val),
|
666 + | Err(_) => env::remove_var("HTTP_PROXY"),
|
667 + | }
|
668 + | }
|
669 + |
|
670 + | #[test]
|
671 + | fn test_build_proxy_url_without_auth() {
|
672 + | let uri = "http://proxy.example.com:8080".parse().unwrap();
|
673 + | let url = ProxyConfig::build_proxy_url(uri, None);
|
674 + | assert_eq!(url, "http://proxy.example.com:8080/");
|
675 + | }
|
676 + |
|
677 + | #[test]
|
678 + | fn test_build_proxy_url_with_auth() {
|
679 + | let uri = "http://proxy.example.com:8080".parse().unwrap();
|
680 + | let auth = ProxyAuth {
|
681 + | username: "user".to_string(),
|
682 + | password: "pass".to_string(),
|
683 + | };
|
684 + | let url = ProxyConfig::build_proxy_url(uri, Some(auth));
|
685 + | assert_eq!(url, "http://user:pass@proxy.example.com:8080/");
|
686 + | }
|
687 + |
|
688 + | #[test]
|
689 + | fn test_build_proxy_url_with_existing_auth() {
|
690 + | let uri = "http://existing:creds@proxy.example.com:8080"
|
691 + | .parse()
|
692 + | .unwrap();
|
693 + | let auth = ProxyAuth {
|
694 + | username: "user".to_string(),
|
695 + | password: "pass".to_string(),
|
696 + | };
|
697 + | let url = ProxyConfig::build_proxy_url(uri, Some(auth));
|
698 + | // Should not override existing auth
|
699 + | assert_eq!(url, "http://existing:creds@proxy.example.com:8080/");
|
700 + | }
|
701 + |
|
702 + | #[test]
|
703 + | #[serial_test::serial]
|
704 + | fn test_into_hyper_util_matcher_from_env() {
|
705 + | // Save original environment
|
706 + | let original_http = env::var("HTTP_PROXY");
|
707 + | env::set_var("HTTP_PROXY", "http://test-proxy:8080");
|
708 + |
|
709 + | let config = ProxyConfig::from_env();
|
710 + | let matcher = config.into_hyper_util_matcher();
|
711 + |
|
712 + | // Test that the matcher intercepts HTTP requests
|
713 + | let test_uri = "http://example.com".parse().unwrap();
|
714 + | let intercept = matcher.intercept(&test_uri);
|
715 + | assert!(intercept.is_some());
|
716 + |
|
717 + | // Restore original environment
|
718 + | match original_http {
|
719 + | Ok(val) => env::set_var("HTTP_PROXY", val),
|
720 + | Err(_) => env::remove_var("HTTP_PROXY"),
|
721 + | }
|
722 + | }
|
723 + |
|
724 + | #[test]
|
725 + | fn test_into_hyper_util_matcher_http() {
|
726 + | let config = ProxyConfig::http("http://proxy.example.com:8080").unwrap();
|
727 + | let matcher = config.into_hyper_util_matcher();
|
728 + |
|
729 + | // Test that the matcher intercepts HTTP requests
|
730 + | let test_uri = "http://example.com".parse().unwrap();
|
731 + | let intercept = matcher.intercept(&test_uri);
|
732 + | assert!(intercept.is_some());
|
733 + | // The intercept URI might be normalized
|
734 + | assert!(intercept
|
735 + | .unwrap()
|
736 + | .uri()
|
737 + | .to_string()
|
738 + | .starts_with("http://proxy.example.com:8080"));
|
739 + |
|
740 + | // Test that it doesn't intercept HTTPS requests
|
741 + | let https_uri = "https://example.com".parse().unwrap();
|
742 + | let https_intercept = matcher.intercept(&https_uri);
|
743 + | assert!(https_intercept.is_none());
|
744 + | }
|
745 + |
|
746 + | #[test]
|
747 + | fn test_into_hyper_util_matcher_with_auth() {
|
748 + | let config = ProxyConfig::http("http://proxy.example.com:8080")
|
749 + | .unwrap()
|
750 + | .with_basic_auth("user", "pass");
|
751 + | let matcher = config.into_hyper_util_matcher();
|
752 + |
|
753 + | // Test that the matcher intercepts HTTP requests
|
754 + | let test_uri = "http://example.com".parse().unwrap();
|
755 + | let intercept = matcher.intercept(&test_uri);
|
756 + | assert!(intercept.is_some());
|
757 + |
|
758 + | let intercept = intercept.unwrap();
|
759 + | // The proxy URI should contain the host (auth is handled separately)
|
760 + | assert!(intercept
|
761 + | .uri()
|
762 + | .to_string()
|
763 + | .contains("proxy.example.com:8080"));
|
764 + |
|
765 + | // Test that basic auth is available
|
766 + | assert!(intercept.basic_auth().is_some());
|
767 + | }
|
768 + |
|
769 + | #[test]
|
770 + | fn test_into_hyper_util_matcher_disabled() {
|
771 + | let config = ProxyConfig::disabled();
|
772 + | let matcher = config.into_hyper_util_matcher();
|
773 + |
|
774 + | // Test that the matcher doesn't intercept any requests
|
775 + | let test_uri = "http://example.com".parse().unwrap();
|
776 + | let intercept = matcher.intercept(&test_uri);
|
777 + | assert!(intercept.is_none());
|
778 + | }
|
779 + |
|
780 + | #[test]
|
781 + | #[serial_test::serial]
|
782 + | fn test_requires_tls_detection() {
|
783 + | // HTTP proxy should not require TLS
|
784 + | let http_config = ProxyConfig::http("http://proxy.example.com:8080").unwrap();
|
785 + | assert!(!http_config.requires_tls());
|
786 + |
|
787 + | // HTTPS proxy URL should require TLS
|
788 + | let https_config = ProxyConfig::http("https://proxy.example.com:8080").unwrap();
|
789 + | assert!(https_config.requires_tls());
|
790 + |
|
791 + | // All proxy with HTTP URL should not require TLS
|
792 + | let all_http_config = ProxyConfig::all("http://proxy.example.com:8080").unwrap();
|
793 + | assert!(!all_http_config.requires_tls());
|
794 + |
|
795 + | // Environment config with HTTPS proxy should require TLS
|
796 + | env::set_var("HTTP_PROXY", "https://proxy.example.com:8080");
|
797 + | let env_config = ProxyConfig::from_env();
|
798 + | assert!(env_config.requires_tls()); // Now detects HTTPS in env vars
|
799 + | env::remove_var("HTTP_PROXY");
|
800 + |
|
801 + | // Environment config with HTTP proxy should not require TLS
|
802 + | env::set_var("HTTP_PROXY", "http://proxy.example.com:8080");
|
803 + | let env_config = ProxyConfig::from_env();
|
804 + | assert!(!env_config.requires_tls());
|
805 + | env::remove_var("HTTP_PROXY");
|
806 + |
|
807 + | // Disabled config should not require TLS
|
808 + | let disabled_config = ProxyConfig::disabled();
|
809 + | assert!(!disabled_config.requires_tls());
|
810 + | }
|
811 + | }
|