use std::sync::Arc; use std::time::{Duration, Instant}; use reqlite_core::http::{ BodyStore, HttpClient, HttpError, RequestOptions, ResolvedBody, ResolvedPart, ResolvedPartBody, ResolvedRequest, }; use reqlite_core::CancelToken; use reqlite_http_curl::CurlClient; use wiremock::matchers::{body_string, body_string_contains, header, method, path}; use wiremock::{Mock, MockServer, ResponseTemplate}; fn req(m: &str, url: String) -> ResolvedRequest { ResolvedRequest { method: m.into(), url, headers: Vec::new(), body: ResolvedBody::None, options: RequestOptions::default(), } } async fn send(r: ResolvedRequest) -> Result { CurlClient::new().send(r, CancelToken::new()).await } fn body_text(b: &BodyStore) -> String { String::from_utf8(b.read_all().unwrap()).unwrap() } #[tokio::test] async fn every_method_with_and_without_body() { let server = MockServer::start().await; for m in ["GET", "DELETE", "OPTIONS", "HEAD"] { Mock::given(method(m)) .and(path("/m")) .respond_with(ResponseTemplate::new(200).insert_header("x-method", m)) .mount(&server) .await; } for m in ["POST", "PUT", "PATCH", "DELETE"] { Mock::given(method(m)) .and(path("/b")) .and(body_string(format!("body for {m}"))) .respond_with(ResponseTemplate::new(201).set_body_string(format!("got {m}"))) .mount(&server) .await; } for m in ["GET", "DELETE", "OPTIONS", "HEAD"] { let r = send(req(m, format!("{}/m", server.uri()))).await.unwrap(); assert_eq!(r.status, 200, "{m}"); assert_eq!(r.header("x-method"), Some(m)); } for m in ["POST", "PUT", "PATCH", "DELETE"] { let mut r = req(m, format!("{}/b", server.uri())); r.body = ResolvedBody::Bytes(format!("body for {m}").into_bytes()); let resp = send(r).await.unwrap(); assert_eq!(resp.status, 201, "{m}"); assert_eq!(resp.reason, "Created"); assert_eq!(body_text(&resp.body), format!("got {m}")); assert_eq!(resp.http_version, "HTTP/1.1"); assert!(resp.timings.total > Duration::ZERO); } } #[tokio::test] async fn sends_headers_and_file_and_multipart_bodies() { let server = MockServer::start().await; Mock::given(method("PUT")) .and(path("/file")) .and(header("x-test", "yes")) .and(body_string("from disk")) .respond_with(ResponseTemplate::new(200)) .mount(&server) .await; Mock::given(method("POST")) .and(path("/multi")) .and(body_string_contains("name=\"field\"")) .and(body_string_contains("value-1")) .and(body_string_contains("filename=\"up.txt\"")) .and(body_string_contains("file content")) .respond_with(ResponseTemplate::new(200)) .mount(&server) .await; let dir = tempfile::tempdir().unwrap(); let f = dir.path().join("up.txt"); std::fs::write(&f, "from disk").unwrap(); let mut r = req("PUT", format!("{}/file", server.uri())); r.headers.push(("X-Test".into(), "yes".into())); r.body = ResolvedBody::File(f.clone()); assert_eq!(send(r).await.unwrap().status, 200); std::fs::write(&f, "file content").unwrap(); let mut r = req("POST", format!("{}/multi", server.uri())); r.headers .push(("Content-Type".into(), "multipart/form-data; boundary=X".into())); r.body = ResolvedBody::Multipart(vec![ ResolvedPart { name: "field".into(), filename: None, content_type: None, body: ResolvedPartBody::Bytes(b"value-1".to_vec()), }, ResolvedPart { name: "upload".into(), filename: Some("up.txt".into()), content_type: Some("text/plain".into()), body: ResolvedPartBody::File(f), }, ]); assert_eq!(send(r).await.unwrap().status, 200); } #[tokio::test] async fn redirects_follow_or_not() { let server = MockServer::start().await; Mock::given(path("/old")) .respond_with(ResponseTemplate::new(302).insert_header("location", "/new")) .mount(&server) .await; Mock::given(path("/new")) .respond_with(ResponseTemplate::new(200).set_body_string("new")) .mount(&server) .await; let r = send(req("GET", format!("{}/old", server.uri()))).await.unwrap(); assert_eq!(r.status, 200); assert_eq!(body_text(&r.body), "new"); assert!(r.effective_url.ends_with("/new")); assert!(r.header("location").is_none(), "headers are from the final hop"); let mut nr = req("GET", format!("{}/old", server.uri())); nr.options.follow_redirects = false; let r = send(nr).await.unwrap(); assert_eq!(r.status, 302); assert_eq!(r.header("location"), Some("/new")); } #[tokio::test] async fn timeout() { let server = MockServer::start().await; Mock::given(path("/slow")) .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(3))) .mount(&server) .await; let mut r = req("GET", format!("{}/slow", server.uri())); r.options.timeout = Some(Duration::from_millis(300)); let start = Instant::now(); assert_eq!(send(r).await.unwrap_err(), HttpError::Timeout); assert!(start.elapsed() < Duration::from_secs(2)); } #[tokio::test] async fn cancel_mid_transfer_ends_the_task_within_200ms() { let server = MockServer::start().await; Mock::given(path("/slow")) .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(10))) .mount(&server) .await; let cancel = CancelToken::new(); let task = tokio::spawn({ let cancel = cancel.clone(); let r = req("GET", format!("{}/slow", server.uri())); async move { CurlClient::new().send(r, cancel).await } }); tokio::time::sleep(Duration::from_millis(200)).await; let cancelled_at = Instant::now(); cancel.cancel(); let res = task.await.unwrap(); assert_eq!(res.unwrap_err(), HttpError::Cancelled); assert!( cancelled_at.elapsed() < Duration::from_millis(200), "took {:?}", cancelled_at.elapsed() ); } #[tokio::test] async fn twenty_concurrent_requests() { let server = MockServer::start().await; Mock::given(path("/c")) .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_millis(300))) .expect(20) .mount(&server) .await; let client = Arc::new(CurlClient::new()); let start = Instant::now(); let tasks: Vec<_> = (0..20) .map(|_| { let client = client.clone(); let r = req("GET", format!("{}/c", server.uri())); tokio::spawn(async move { client.send(r, CancelToken::new()).await }) }) .collect(); for t in tasks { assert_eq!(t.await.unwrap().unwrap().status, 200); } // In parallel, not one after the other (20 × 300 ms = 6 s). assert!(start.elapsed() < Duration::from_secs(3), "{:?}", start.elapsed()); } #[tokio::test] async fn large_bodies_spill_to_a_temp_file() { let server = MockServer::start().await; let big = vec![b'x'; 6 * 1024 * 1024]; Mock::given(path("/big")) .respond_with(ResponseTemplate::new(200).set_body_bytes(big.clone())) .mount(&server) .await; Mock::given(path("/small")) .respond_with(ResponseTemplate::new(200).set_body_string("small")) .mount(&server) .await; let r = send(req("GET", format!("{}/big", server.uri()))).await.unwrap(); assert_eq!(r.size, big.len() as u64); let BodyStore::TempFile(path) = &r.body else { panic!("expected a spilled body") }; let path = path.to_path_buf(); assert_eq!(r.body.read_all().unwrap().len(), big.len()); drop(r); assert!(!path.exists(), "temp file is removed with the response"); let r = send(req("GET", format!("{}/small", server.uri()))).await.unwrap(); assert_eq!(r.body.in_memory(), Some(&b"small"[..])); } #[tokio::test] async fn connection_errors_are_classified() { // Nothing listens on port 9 (discard) on CI-less dev machines. let mut r = req("GET", "http://127.0.0.1:9/".into()); r.options.connect_timeout = Some(Duration::from_secs(2)); assert!(matches!(send(r).await.unwrap_err(), HttpError::Connect(_))); let r = req("GET", "http://does-not-exist.invalid/".into()); assert!(matches!(send(r).await.unwrap_err(), HttpError::Dns(_))); let r = req("GET", "nope://x".into()); assert!(matches!(send(r).await.unwrap_err(), HttpError::InvalidUrl(_))); } mod tls { use super::*; use rcgen::{BasicConstraints, CertificateParams, CertifiedIssuer, DnType, IsCa, KeyPair}; use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; /// A one-shot-per-connection HTTPS server with a certificate signed by a /// fresh CA. Returns the port and the CA certificate in PEM. async fn https_server() -> (u16, String) { let mut ca_params = CertificateParams::new(Vec::::new()).unwrap(); ca_params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained); // rcgen's default subject is the same for every cert; a CA with the // leaf's name would make the leaf look self-signed. ca_params.distinguished_name.push(DnType::CommonName, "reqlite test CA"); let ca = CertifiedIssuer::self_signed(ca_params, KeyPair::generate().unwrap()).unwrap(); let leaf_key = KeyPair::generate().unwrap(); let leaf = CertificateParams::new(vec!["localhost".to_string()]) .unwrap() .signed_by(&leaf_key, &ca) .unwrap(); let config = rustls::ServerConfig::builder() .with_no_client_auth() .with_single_cert( vec![CertificateDer::from(leaf.der().to_vec())], PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(leaf_key.serialize_der())), ) .unwrap(); let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(config)); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); tokio::spawn(async move { loop { let Ok((tcp, _)) = listener.accept().await else { return }; let acceptor = acceptor.clone(); tokio::spawn(async move { let Ok(mut tls) = acceptor.accept(tcp).await else { return; }; let mut buf = vec![0u8; 4096]; let _ = tls.read(&mut buf).await; let _ = tls .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\nsecure") .await; let _ = tls.shutdown().await; }); } }); (port, ca.pem()) } #[tokio::test] async fn self_signed_certificate() { let (port, ca_pem) = https_server().await; let url = format!("https://localhost:{port}/"); // Untrusted by default. let err = send(req("GET", url.clone())).await.unwrap_err(); assert!(matches!(err, HttpError::Tls(_)), "{err:?}"); // Verification off. let mut r = req("GET", url.clone()); r.options.verify_tls = false; let resp = send(r).await.unwrap(); assert_eq!(body_text(&resp.body), "secure"); assert!(resp.timings.tls >= resp.timings.connect); // Custom CA file. let dir = tempfile::tempdir().unwrap(); let ca = dir.path().join("ca.pem"); std::fs::write(&ca, ca_pem).unwrap(); let mut r = req("GET", url); r.options.ca_file = Some(ca); assert_eq!(send(r).await.unwrap().status, 200); } }