Clawspec
A Rust library for generating OpenAPI specifications from your HTTP client test code. Write tests, get documentation.
Overview
Clawspec automatically generates OpenAPI documentation by observing HTTP client interactions in your tests. Instead of maintaining separate API documentation, your tests become the source of truth.
Key Features
- ๐งช Test-Driven Documentation - Generate specs from integration tests
- ๐ Type Safety - Leverage Rust's type system for accurate schemas
- ๐ Zero Runtime Overhead - Documentation generation only runs during tests
- ๐ ๏ธ Framework Agnostic - Works with any async HTTP server
- ๐ OpenAPI 3.1 Compliant - Generate standard-compliant specifications
- ๐ Authentication Support - Bearer, Basic, and API Key authentication
- ๐ช Cookie Support - Full cookie parameter handling and documentation
- ๐ Parameter Styles - Complete OpenAPI 3.1.0 parameter style support
Quick Start
Add to your Cargo.toml:
[]
= "0.1.4"
[]
= { = "1", = ["full"] }
Basic Example with ApiClient
use ApiClient;
use ;
use ToSchema;
async
Test Server Example with TestClient
For testing complete web applications. See the axum example for a full working implementation:
use ;
use TcpListener;
;
async
Core Concepts
ApiClient
The main HTTP client that captures request/response schemas:
- Builder pattern for configuration
- Automatic schema extraction from Rust types
- Flexible parameter handling (path, query, headers, cookies)
- Authentication support (Bearer, Basic, API Key)
- Status code validation with ranges and specific codes
- OpenAPI 3.1.0 parameter styles support
TestClient
A test-focused wrapper providing:
- Automatic server lifecycle management
- Health checking with retries
- Integrated OpenAPI generation
- Framework-agnostic design
Advanced Usage
Parameter Handling
use ;
let path = from
.add_param
.add_param;
let query = new
.add_param
.add_param;
let headers = new
.add_header
.add_header;
let cookies = new
.add_cookie
.add_cookie;
let response = client
.get?
.with_query
.with_headers
.with_cookies
.exchange
.await?;
Status Code Validation
By default, requests expect status codes in the range 200-499. You can customize this:
use ;
// Accept specific codes
client.post?
.with_expected_status_codes
.await?;
// Accept ranges
client.get?
.with_expected_status_codes
.await?;
// Complex patterns
client.delete?
.with_expected_status_codes
.await?;
Schema Registration
use ;
// Register schemas for better documentation
register_schemas!;
Authentication
Clawspec supports various authentication methods with enhanced security features:
use ;
// Bearer token authentication
let client = builder
.with_host
.with_authentication
.build?;
// Basic authentication
let client = builder
.with_authentication
.build?;
// API key authentication
let client = builder
.with_authentication
.build?;
// Per-request authentication override
let response = client
.get?
.with_authentication
.await?;
// Disable authentication for public endpoints
let public_data = client
.get?
.with_authentication_none
.await?;
Security Features
- Memory Protection: Sensitive credentials are automatically cleared from memory when no longer needed
- Debug Safety: Authentication data is redacted in debug output to prevent accidental logging
- Display Masking: Credentials are masked when displayed (e.g.,
Bearer abcd...789) - Granular Error Handling: Detailed authentication error types for better debugging
Security Best Practices
- Store credentials securely using environment variables or secret management tools
- Rotate tokens regularly
- Use HTTPS for all authenticated requests
- Never log authentication headers or credentials
Cookie Support
Handle cookie parameters with full OpenAPI documentation:
use ;
let cookies = new
.add_cookie
.add_cookie
.add_cookie;
let response = client
.get?
.with_cookies
.await?;
Integration Examples
With Axum
For a complete working example, see the axum example implementation.
use ;
use ;
Configuration
TestServerConfig
Configure test server behavior:
use TestServerConfig;
use Duration;
let config = TestServerConfig ;
Error Handling
The library provides comprehensive error types:
ApiClientError- HTTP client errors (includes authentication errors)AuthenticationError- Granular authentication failure detailsTestAppError- Test server errors
All errors implement standard error traits and provide detailed context for debugging.
Best Practices
- Write focused tests - Each test should document specific endpoints
- Use descriptive types - Well-named structs generate better documentation
- Register schemas - Explicitly register types for complete documentation
- Validate status codes - Be explicit about expected responses
- Organize tests - Group related endpoint tests together
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Note: This project has been developed with assistance from Claude Code. All AI-generated code has been carefully reviewed, tested, and validated to ensure quality, security, and adherence to Rust best practices.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Acknowledgments
Built with excellent crates from the Rust ecosystem: