| Crates.io | http-endpoint-server-harness |
| lib.rs | http-endpoint-server-harness |
| version | 0.1.1 |
| created_at | 2026-01-17 15:07:17.382578+00 |
| updated_at | 2026-01-17 15:15:18.010937+00 |
| description | HTTP endpoint server harness for testing mock endpoints |
| homepage | |
| repository | https://github.com/nathan-poncet/rust-server-harness |
| max_upload_size | |
| id | 2050580 |
| size | 117,697 |
A Rust library for creating mock HTTP servers in your integration tests. Instead of mocking your HTTP client, spin up a real server that responds exactly as you configure it.
When testing code that calls external HTTP APIs, you need to verify that:
Traditional approaches have drawbacks:
| Approach | Problem |
|---|---|
| Mock the HTTP client | Doesn't test actual serialization, headers, or network code |
| Use a shared test server | Flaky tests, shared state issues, hard to customize per test |
| Record/replay (VCR) | Brittle when APIs change, hard to test error scenarios |
Server Harness gives you:
[dev-dependencies]
http-endpoint-server-harness = "0.1"
tokio = { version = "1", features = ["full"] }
reqwest = "0.12"
use http_endpoint_server_harness::prelude::*;
use std::net::SocketAddr;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), HarnessError> {
let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
// Spawn a task to make HTTP requests
let requests_task = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
let client = reqwest::Client::new();
client.get(format!("http://{}/api/users", addr))
.send()
.await
.unwrap();
});
// Build and execute the scenario
let collected = ScenarioBuilder::new()
.server(Axum::bind(addr))
.collector(DefaultCollector::new())
.endpoint(
Endpoint::new("/api/users", Method::Get)
.with_handler(Handler::from_json(&serde_json::json!({
"users": [{"id": 1, "name": "Alice"}]
})))
)
.build()
.execute()
.await?;
requests_task.await.unwrap();
// Assert on collected requests
assert_eq!(collected.len(), 1);
assert_eq!(collected[0].path, "/api/users");
Ok(())
}
Create handlers that respond dynamically based on the request:
let endpoint = Endpoint::new("/api/echo", Method::Post)
.with_handler(Handler::dynamic(|ctx| {
Response::ok()
.with_json_body(&serde_json::json!({
"echoed": ctx.body_as_str().unwrap_or(""),
"method": format!("{:?}", ctx.method),
"path": ctx.path
}))
.unwrap()
}));
Define multiple handlers for the same endpoint - each subsequent request uses the next handler:
let endpoint = Endpoint::new("/api/counter", Method::Get)
.with_handler(Handler::from_json(&json!({"count": 1})))
.with_handler(Handler::from_json(&json!({"count": 2})))
.with_handler(Handler::from_json(&json!({"count": 3})));
// Custom status code
let handler = Handler::new(Response::new(201).with_body("Created"));
// Custom headers
let handler = Handler::new(
Response::ok()
.with_header("X-Custom-Header", "value")
.with_json_body(&json!({"success": true}))
.unwrap()
);
βββββββββββββββββββ ββββββββββββββββββββ
β Your Code β GET /api/users β Mock Server β
β (HTTP Client) βββββββββββββββββββββΆβ (Axum-based) β
β β β β
β ββββββββββββββββββββββ Returns JSON β
β β 200 OK + JSON β you configured β
βββββββββββββββββββ ββββββββββββββββββββ
β
βΌ
Auto-shutdown when
all handlers consumed
β
βΌ
ββββββββββββββββββββ
β Collected Requestsβ
β for assertions β
ββββββββββββββββββββ
MIT - see LICENSE for details.