Module test_client

Source
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

§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§

TestClient
A generic test client for async server testing.
TestServerConfig
Configuration for test server behavior and client setup.

Enums§

HealthStatus
A trait for implementing test server abstractions for different web frameworks.
TestAppError
Error types for test client operations.

Traits§

TestServer