Skip to content

Commit beb8aa4

Browse files
author
Eduard Miller
committed
download: Add test for socks5 feature availability
1 parent 2fc3243 commit beb8aa4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

download/tests/read-proxy-env.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#![cfg(feature = "reqwest-backend")]
22

33
use std::env::{remove_var, set_var};
4+
use std::error::Error;
5+
use std::net::TcpListener;
6+
use std::sync::atomic::{AtomicUsize, Ordering};
7+
use std::thread;
8+
use std::time::Duration;
49

510
use env_proxy::for_url;
11+
use reqwest::{blocking::Client, Proxy};
612
use url::Url;
713

814
fn scrub_env() {
@@ -28,3 +34,38 @@ fn read_basic_proxy_params() {
2834
Some(("proxy.example.com".to_string(), 8080))
2935
);
3036
}
37+
38+
// Tests to verify if socks feature is available and being used
39+
#[test]
40+
fn socks_proxy_request() {
41+
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
42+
43+
scrub_env();
44+
set_var("all_proxy", "socks5://127.0.0.1:1080");
45+
46+
thread::spawn(move || {
47+
let listener = TcpListener::bind("127.0.0.1:1080").unwrap();
48+
let incoming = listener.incoming();
49+
for _ in incoming {
50+
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
51+
}
52+
});
53+
54+
let env_proxy = |url: &Url| for_url(&url).to_url();
55+
let url = Url::parse("http://example.org").unwrap();
56+
57+
let client = Client::builder()
58+
.proxy(Proxy::custom(env_proxy))
59+
.timeout(Duration::from_secs(1))
60+
.build()
61+
.unwrap();
62+
let res = client.get(url.as_str()).send();
63+
64+
if let Err(e) = res {
65+
let s = e.source().unwrap();
66+
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
67+
assert!(s.to_string().contains("socks connect error"));
68+
} else {
69+
panic!("Socks proxy was ignored")
70+
}
71+
}

0 commit comments

Comments
 (0)