Expand description
Generic test client framework for async server testing.
This module provides a generic testing framework that works with any async server
implementation through the TestServer trait. It allows you to start a server,
make API calls, and generate OpenAPI specifications from your tests.
§Core Components
TestClient<T>: Generic test client that wraps any server implementingTestServerTestServer: Trait for server implementations (Axum, Warp, actix-web, etc.)TestServerConfig: Configuration for test behaviorTestAppError: Error types for test operations
§Quick Start
For a complete working example, see the axum example.
use clawspec_core::test_client::{TestClient, TestServer, TestServerConfig};
use std::net::TcpListener;
#[derive(Debug)]
struct MyTestServer;
impl TestServer for MyTestServer {
type Error = std::io::Error;
async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
// Start your server here
listener.set_nonblocking(true)?;
let _tokio_listener = tokio::net::TcpListener::from_std(listener)?;
// Your server startup logic
Ok(())
}
}
#[tokio::test]
async fn test_my_api() -> Result<(), Box<dyn std::error::Error>> {
let mut test_client = TestClient::start(MyTestServer).await?;
let response = test_client
.get("/api/users")?
.await?;
// Generate OpenAPI documentation
test_client.write_openapi("openapi.yml").await?;
Ok(())
}Structs§
- Test
Client - A generic test client for async server testing.
- Test
Server Config - Configuration for test server behavior and client setup.
Enums§
- Health
Status - A trait for implementing test server abstractions for different web frameworks.
- Test
AppError - Error types for test client operations.