Expand description
HTTP Endpoint Server Harness
A test harness for mocking HTTP endpoints with predefined responses. The server automatically shuts down once all handlers have been called.
§Example
use http_endpoint_server_harness::prelude::*;
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> Result<(), HarnessError> {
// Define a fixed address for the server
let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
// Spawn a task to make HTTP requests
let requests_task = tokio::spawn(async move {
// Wait for server to be ready (in real code, add proper retry logic)
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let client = reqwest::Client::new();
let _response = client
.get(format!("http://{}/api/users", addr))
.send()
.await
.unwrap();
});
// Build and execute a scenario using the builder pattern
let collected = ScenarioBuilder::new()
.server(Axum::bind(addr))
.collector(DefaultCollector::new())
.endpoint(
Endpoint::new("/api/users", Method::Get)
.with_handler(Handler::from_json(&json!({"id": 1})))
)
.build()
.execute()
.await?;
requests_task.await.unwrap();
// Server has automatically shut down, collected contains all requests
for req in &collected {
println!("Received: {} {}", req.method, req.path);
}
Ok(())
}Re-exports§
pub use error::HarnessError;
Modules§
Structs§
- Axum
- Axum-based HTTP server implementation
- Default
Collector - Default collector implementation that collects requests into a Vec