use awc::{http::header::HeaderValue, Client, Connector, SendClientRequest}; use rustls::{client::ClientConfig, Certificate, RootCertStore}; use rustls_pemfile::certs; use std::{ fs::{read, File}, io::BufReader, sync::Arc, }; use vacuna::Vacuna; fn get_host(c: &Client, path: &str) -> SendClientRequest { c.get(path).insert_header(("Host", "example.com")).send() } fn get_with_host(c: &Client, path: &str, host: &str) -> SendClientRequest { c.get(path).insert_header(("Host", host)).send() } fn get(c: &Client, path: &str) -> SendClientRequest { c.get(path).send() } #[actix_rt::test] async fn test_simple() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/simple_site/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9000/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/simple_site/files/index.html").unwrap() ); let mut result = get_host(&c, "http://127.0.0.1:9000/").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/simple_site/files/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_multiple_folder() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/multiple_folder/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9001/first/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!(result.headers().get("Access-Control-Allow-Origin"), None); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/multiple_folder/first/index.html").unwrap() ); let mut result = get_host(&c, "http://127.0.0.1:9001/second/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.headers().get("Access-Control-Allow-Origin"), Some(&HeaderValue::from_static("*")) ); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/multiple_folder/second/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_simple_ssl() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/simple_ssl/conf.kdl").unwrap(); let mut cert_store = RootCertStore::empty(); let cert_file = &mut BufReader::new(File::open("./fixtures/simple_ssl/CA.pem").unwrap()); let cert_chain = certs(cert_file) .unwrap() .into_iter() .map(Certificate) .collect::>(); cert_store.add(&cert_chain[0]).unwrap(); let rls = ClientConfig::builder() .with_safe_defaults() .with_root_certificates(cert_store) .with_no_client_auth(); let connector = Connector::new().rustls_021(Arc::new(rls)); let c = Client::builder().connector(connector).finish(); let mut result = get(&c, "https://localhost:9002/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/simple_ssl/files/index.html").unwrap() ); let mut result = get(&c, "https://localhost:9002/").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/simple_ssl/files/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_ssl_folders() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/ssl_sites_folder/conf.kdl").unwrap(); let mut cert_store = RootCertStore::empty(); let cert_file = &mut BufReader::new(File::open("./fixtures/ssl_sites_folder/certs/localhost/CA.pem").unwrap()); let cert_chain = certs(cert_file) .unwrap() .into_iter() .map(Certificate) .collect::>(); cert_store.add(&cert_chain[0]).unwrap(); let rls = ClientConfig::builder() .with_safe_defaults() .with_root_certificates(cert_store) .with_no_client_auth(); let connector = Connector::new().rustls_021(Arc::new(rls)); let c = Client::builder().connector(connector).finish(); let mut result = get(&c, "https://localhost:9007/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/ssl_sites_folder/files/index.html").unwrap() ); let mut result = get(&c, "https://localhost:9007/").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/ssl_sites_folder/files/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_ssl_folders_acme() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/ssl_sites_folder_acme/conf.kdl").unwrap(); let mut cert_store = RootCertStore::empty(); let cert_file = &mut BufReader::new(File::open("./fixtures/ssl_sites_folder_acme/certs/localhost/CA.pem").unwrap()); let cert_chain = certs(cert_file) .unwrap() .into_iter() .map(Certificate) .collect::>(); cert_store.add(&cert_chain[0]).unwrap(); let rls = ClientConfig::builder() .with_safe_defaults() .with_root_certificates(cert_store) .with_no_client_auth(); let connector = Connector::new().rustls_021(Arc::new(rls)); let c = Client::builder().connector(connector).finish(); let mut result = get(&c, "https://localhost:9008/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/ssl_sites_folder_acme/files/index.html").unwrap() ); let mut result = get(&c, "https://localhost:9008/").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/ssl_sites_folder_acme/files/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_simple_proxy() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/simple_proxy/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9003/home/index.html?abc=dd") .await .unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/simple_proxy/files/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_log() { let _ = pretty_env_logger::try_init(); //capture_logger::begin_capture(); { let vacuna = Vacuna::start_background_from_file("./fixtures/logging/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9005/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/logging/files/index.html").unwrap() ); vacuna.stop().await; } /* loop { use log::Level; let log = capture_logger::pop_captured().unwrap(); println!("{:?} {}", log.level(), log.message() ); if log.level() == Level::Info { assert!(log.message().contains("200")); break; } } capture_logger::end_capture(); */ } #[actix_rt::test] async fn test_auto() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/auto_sites/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9004/index.html").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/auto_sites/example.com/index.html").unwrap() ); let mut result = get_with_host(&c, "http://127.0.0.1:9004/index.html", "example.org") .await .unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/auto_sites/example.org/index.html").unwrap() ); vacuna.stop().await; } #[actix_rt::test] async fn test_auto_config() { let _ = pretty_env_logger::try_init(); let vacuna = Vacuna::start_background_from_file("./fixtures/auto_sites_config/conf.kdl").unwrap(); let c = Client::default(); let mut result = get_host(&c, "http://127.0.0.1:9006/").await.unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/auto_sites_config/example.com/other.html").unwrap() ); let mut result = get_with_host(&c, "http://127.0.0.1:9006/", "example.org") .await .unwrap(); assert!(result.status().is_success()); assert_eq!( result.body().await.unwrap().to_vec(), read("./fixtures/auto_sites_config/example.org/other.html").unwrap() ); vacuna.stop().await; }