use openssl::ssl; use std::time::Duration; fn client() -> awc::Client { static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); let connector = ssl::SslConnector::builder(ssl::SslMethod::tls_client()).unwrap(); awc::Client::builder() .header("User-Agent", USER_AGENT) .connector( awc::Connector::new() .ssl(connector.build()) .timeout(Duration::from_secs(5)) .finish(), ) .finish() } #[actix_rt::test] async fn test_get() -> actix_web::Result<()> { let http = client(); for &(url, status) in &[ ("https://www.rust-lang.org", Some(200)), ("http://www.rust-lang.org", Some(301)), ("www.rust-lang.org", None), ] { println!("\nGET {}", url); if let Some(expected) = status { let status = http.get(url).send().await?.status(); println!(" with status: {}", status); assert_eq!(expected, status); } else { println!(" failed"); } } Ok(()) } #[actix_rt::test] async fn test_post() -> actix_web::Result<()> { let http = client(); static CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME"); static CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); let info = serde_json::json!({ "name": CARGO_PKG_NAME, "version": CARGO_PKG_VERSION, }); for &(url, status) in &[ ("https://httpbin.org/post", Some(200)), ("http://httpbin.org/post", Some(200)), ("httpbin.org/post", None), ] { println!("\nPOST {}", url); if let Some(expected) = status { let status = http.post(url).send_json(&info).await?.status(); println!(" with status: {}", status); assert_eq!(expected, status); } else { println!(" failed"); } } Ok(()) }