Skip to content

Commit

Permalink
download: Add test for socks5 feature availability
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Miller committed Aug 31, 2020
1 parent 2fc3243 commit beb8aa4
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions download/tests/read-proxy-env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#![cfg(feature = "reqwest-backend")]

use std::env::{remove_var, set_var};
use std::error::Error;
use std::net::TcpListener;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

use env_proxy::for_url;
use reqwest::{blocking::Client, Proxy};
use url::Url;

fn scrub_env() {
Expand All @@ -28,3 +34,38 @@ fn read_basic_proxy_params() {
Some(("proxy.example.com".to_string(), 8080))
);
}

// Tests to verify if socks feature is available and being used
#[test]
fn socks_proxy_request() {
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);

scrub_env();
set_var("all_proxy", "socks5://127.0.0.1:1080");

thread::spawn(move || {
let listener = TcpListener::bind("127.0.0.1:1080").unwrap();
let incoming = listener.incoming();
for _ in incoming {
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
}
});

let env_proxy = |url: &Url| for_url(&url).to_url();
let url = Url::parse("http://example.org").unwrap();

let client = Client::builder()
.proxy(Proxy::custom(env_proxy))
.timeout(Duration::from_secs(1))
.build()
.unwrap();
let res = client.get(url.as_str()).send();

if let Err(e) = res {
let s = e.source().unwrap();
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
assert!(s.to_string().contains("socks connect error"));
} else {
panic!("Socks proxy was ignored")
}
}

0 comments on commit beb8aa4

Please sign in to comment.