#[cfg(test)]
mod tests {
use crate::Extrablatt;
use testcontainers::{
core::{IntoContainerPort, WaitFor},
runners::AsyncRunner,
GenericImage,
};
#[tokio::test]
async fn test_proxy_with_squid_container() {
let container = GenericImage::new("ubuntu/squid", "latest")
.with_exposed_port(3128.tcp())
.with_wait_for(WaitFor::message_on_stderr("Accepting HTTP Socket connections"))
.start()
.await;
let container = match container {
Ok(c) => c,
Err(e) => {
eprintln!("Failed to start Squid container (Docker may not be running): {}", e);
return; }
};
let proxy_port = container.get_host_port_ipv4(3128).await.unwrap();
let proxy_url = format!("http://127.0.0.1:{}", proxy_port);
println!("Squid proxy started on: {}", proxy_url);
let result = Extrablatt::builder("https://httpbin.org/ip")
.expect("Failed to create builder")
.proxy(&proxy_url)
.build()
.await;
match result {
Ok(_site) => {
println!("Successfully connected through proxy!");
}
Err(e) => {
println!("Request through proxy result: {:?}", e);
}
}
}
#[tokio::test]
async fn test_invalid_proxy_url() {
let result = Extrablatt::builder("https://example.com")
.expect("Failed to create builder")
.proxy("not-a-valid-proxy-url")
.build()
.await;
assert!(result.is_err(), "Expected error for invalid proxy URL");
println!("Invalid proxy correctly rejected: {:?}", result.err());
}
#[tokio::test]
async fn test_no_proxy_works() {
let result = Extrablatt::builder("https://httpbin.org/ip")
.expect("Failed to create builder")
.build()
.await;
match result {
Ok(_) => println!("Request without proxy succeeded"),
Err(e) => println!("Request without proxy failed (network issue?): {:?}", e),
}
}
}