Add test infrastructure with fixtures, helpers, and integration tests
- Add [lib] target to enable integration test imports - Add rcgen and reqwest dev-dependencies for TLS and HTTP test helpers - Create src/config/test_fixtures.rs with test_static_config() and test_dynamic_config() - Create tests/ with integration tests, HTTP test helper (TestUpstream), and TLS test helper (SelfSignedCert) - Add Clone derives to StaticConfig and related structs for test fixture construction - All existing tests continue to pass
This commit is contained in:
47
tests/helpers/http_test_helper.rs
Normal file
47
tests/helpers/http_test_helper.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub struct TestUpstream {
|
||||
pub addr: SocketAddr,
|
||||
pub shutdown_tx: tokio::sync::oneshot::Sender<()>,
|
||||
}
|
||||
|
||||
impl TestUpstream {
|
||||
pub async fn spawn<F>(handler_factory: F) -> Self
|
||||
where
|
||||
F: FnOnce() -> Router,
|
||||
{
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
|
||||
|
||||
let app = handler_factory();
|
||||
tokio::spawn(async move {
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(async {
|
||||
let _ = shutdown_rx.await;
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
Self { addr, shutdown_tx }
|
||||
}
|
||||
|
||||
pub async fn spawn_ok() -> Self {
|
||||
Self::spawn(|| Router::new().route("/", get(|| async { "ok" }))).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn url(&self) -> String {
|
||||
format!("http://{}", self.addr)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn upstream_addr(&self) -> String {
|
||||
format!("127.0.0.1:{}", self.addr.port())
|
||||
}
|
||||
}
|
||||
2
tests/helpers/mod.rs
Normal file
2
tests/helpers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod http_test_helper;
|
||||
pub mod tls_test_helper;
|
||||
28
tests/helpers/tls_test_helper.rs
Normal file
28
tests/helpers/tls_test_helper.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use rcgen::{CertificateParams, DistinguishedName, KeyPair};
|
||||
|
||||
pub struct SelfSignedCert {
|
||||
pub cert_pem: String,
|
||||
pub key_pem: String,
|
||||
}
|
||||
|
||||
pub fn generate_self_signed_cert(domains: &[&str]) -> SelfSignedCert {
|
||||
let mut params = CertificateParams::new(
|
||||
domains
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
)
|
||||
.unwrap();
|
||||
params.distinguished_name = DistinguishedName::new();
|
||||
params
|
||||
.distinguished_name
|
||||
.push(rcgen::DnType::CommonName, "test.local");
|
||||
|
||||
let key_pair = KeyPair::generate().unwrap();
|
||||
let cert = params.self_signed(&key_pair).unwrap();
|
||||
|
||||
SelfSignedCert {
|
||||
cert_pem: cert.pem(),
|
||||
key_pem: key_pair.serialize_pem(),
|
||||
}
|
||||
}
|
||||
32
tests/integration_test.rs
Normal file
32
tests/integration_test.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
mod helpers;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_upstream_spawn_and_connect() {
|
||||
let upstream = helpers::http_test_helper::TestUpstream::spawn_ok().await;
|
||||
let client = reqwest::Client::new();
|
||||
let resp = client
|
||||
.get(format!("http://127.0.0.1:{}/", upstream.addr.port()))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), reqwest::StatusCode::OK);
|
||||
let _ = upstream.shutdown_tx.send(());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_self_signed_cert_generation() {
|
||||
let cert = helpers::tls_test_helper::generate_self_signed_cert(&["test.local"]);
|
||||
assert!(!cert.cert_pem.is_empty());
|
||||
assert!(!cert.key_pem.is_empty());
|
||||
assert!(cert.cert_pem.contains("BEGIN CERTIFICATE"));
|
||||
assert!(cert.key_pem.contains("BEGIN"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_fixtures() {
|
||||
let static_config = reverse_proxy::config::test_fixtures::test_static_config();
|
||||
assert!(!static_config.listeners.is_empty());
|
||||
|
||||
let dynamic_config = reverse_proxy::config::test_fixtures::test_dynamic_config();
|
||||
assert!(!dynamic_config.sites.is_empty());
|
||||
}
|
||||
Reference in New Issue
Block a user