//! Proxy HTTP (M2.6), provato sul serio: un server locale fa da proxy e il test //! guarda la richiesta che riceve. Niente internet: l'host di destinazione è //! inventato apposta, così se il client provasse a risolverlo il test fallirebbe. use std::io::{Read, Write}; use std::net::TcpListener; use std::thread; use engine::net::{imposta_proxy, Limiti}; /// Server che accetta una connessione, cattura la testa della richiesta e /// risponde con lo stato indicato. fn proxy_che_riceve(stato: u16) -> (u16, std::sync::mpsc::Receiver) { let ascolto = TcpListener::bind("127.0.0.1:0").expect("porta libera"); let porta = ascolto.local_addr().unwrap().port(); let (invio, ricevi) = std::sync::mpsc::channel(); thread::spawn(move || { if let Some(Ok(mut flusso)) = ascolto.incoming().next() { let mut buf = [0u8; 4096]; let n = flusso.read(&mut buf).unwrap_or(0); let testo = String::from_utf8_lossy(&buf[..n]).to_string(); let testa = testo.split("\r\n\r\n").next().unwrap_or("").to_string(); let _ = invio.send(testa); let corpo = if stato == 200 { "ciao" } else { "" }; let risposta = format!( "HTTP/1.1 {} X\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}", stato, corpo.len(), corpo ); let _ = flusso.write_all(risposta.as_bytes()); } }); (porta, ricevi) } #[test] fn http_attraverso_il_proxy_usa_la_forma_assoluta() { let (porta, ricevi) = proxy_che_riceve(200); imposta_proxy(Some(&format!("http://127.0.0.1:{}", porta))); // host inesistente: se il client lo risolvesse, questa richiesta fallirebbe let esito = engine::net::get( "http://esempio-inventato.test/pagina?x=1", &Limiti::default(), ); imposta_proxy(None); let risposta = esito.expect("la risposta arriva dal proxy"); assert_eq!(risposta.status, 200); assert_eq!(risposta.text().unwrap(), "ciao"); let testa = ricevi .recv_timeout(std::time::Duration::from_secs(5)) .unwrap(); let prima = testa.lines().next().unwrap_or(""); assert_eq!( prima, "GET http://esempio-inventato.test/pagina?x=1 HTTP/1.1", "attraverso un proxy http la richiesta si manda in forma assoluta" ); assert!( testa.contains("Host: esempio-inventato.test"), "l'intestazione Host resta quella della destinazione: {}", testa ); } #[test] fn https_apre_il_tunnel_prima_di_parlare() { // il finto proxy rifiuta il tunnel: il client deve dirlo, non fingere let (porta, ricevi) = proxy_che_riceve(403); imposta_proxy(Some(&format!("http://127.0.0.1:{}", porta))); let esito = engine::net::get("https://esempio-inventato.test/pagina", &Limiti::default()); imposta_proxy(None); let errore = esito.expect_err("senza tunnel non c'è risposta"); assert!( errore.contains("tunnel") && errore.contains("403"), "l'errore nomina il tunnel e lo stato: {}", errore ); let testa = ricevi .recv_timeout(std::time::Duration::from_secs(5)) .unwrap(); let prima = testa.lines().next().unwrap_or(""); assert_eq!( prima, "CONNECT esempio-inventato.test:443 HTTP/1.1", "per https il proxy riceve CONNECT sull'host e la porta" ); } #[test] fn le_credenziali_del_proxy_finiscono_nell_intestazione() { let (porta, ricevi) = proxy_che_riceve(200); imposta_proxy(Some(&format!("http://tizio:parola@127.0.0.1:{}", porta))); let esito = engine::net::get("http://destinazione.test/", &Limiti::default()); imposta_proxy(None); esito.expect("richiesta eseguita"); let testa = ricevi .recv_timeout(std::time::Duration::from_secs(5)) .unwrap(); // "tizio:parola" in base64 assert!( testa.contains("Proxy-Authorization: Basic dGl6aW86cGFyb2xh"), "le credenziali vanno codificate in base64: {}", testa ); } #[test] fn senza_proxy_la_destinazione_si_risolve_davvero() { // controllo di non-regressione: senza proxy il client risolve l'host e // fallisce, invece di mandare la richiesta a qualcuno imposta_proxy(None); let errore = engine::net::get("http://esempio-inventato.test/", &Limiti::default()) .expect_err("l'host non esiste"); assert!( errore.contains("risoluzione") || errore.contains("connessione"), "l'errore parla di risoluzione o connessione: {}", errore ); }