pollux/tests/common.rs
Jeena 01bcda10d0 Unify integration test environment and add valid config validation
- Create shared tests/common.rs with TestEnvironment setup
- Simplify gemini_test_client.py to single-request client
- Refactor config validation tests to use common setup
- Add test_valid_config_startup for complete server validation
- Fix clippy warning in main.rs
- Remove unused code and consolidate test infrastructure
2026-01-16 23:59:54 +00:00

59 lines
No EOL
1.6 KiB
Rust

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");
}