use std::path::{Path, PathBuf}; use tempfile::TempDir; pub struct TestEnvironment { pub temp_dir: TempDir, pub config_path: PathBuf, pub cert_path: PathBuf, pub key_path: PathBuf, pub content_path: PathBuf, pub port: u16, } pub fn setup_test_environment() -> TestEnvironment { let temp_dir = TempDir::new().unwrap(); let config_path = temp_dir.path().join("config.toml"); let cert_path = temp_dir.path().join("cert.pem"); let key_path = temp_dir.path().join("key.pem"); let content_path = temp_dir.path().join("content"); // Create content directory and file std::fs::create_dir(&content_path).unwrap(); std::fs::write(content_path.join("test.gmi"), "# Test Gemini content\n").unwrap(); // Generate test certificates generate_test_certificates(temp_dir.path()); // Use a unique port based on process ID to avoid conflicts let port = 1967 + (std::process::id() % 1000) as u16; TestEnvironment { temp_dir, config_path, cert_path, key_path, content_path, port, } } fn generate_test_certificates(temp_dir: &Path) { use std::process::Command; let cert_path = temp_dir.join("cert.pem"); let key_path = temp_dir.join("key.pem"); let status = Command::new("openssl") .args(&[ "req", "-x509", "-newkey", "rsa:2048", "-keyout", &key_path.to_string_lossy(), "-out", &cert_path.to_string_lossy(), "-days", "1", "-nodes", "-subj", "/CN=localhost" ]) .status() .unwrap(); assert!(status.success(), "Failed to generate test certificates"); }