pub enum HealthStatus {
Healthy,
Unhealthy,
Uncheckable,
}
Expand description
A trait for implementing test server abstractions for different web frameworks.
This trait provides a generic interface for launching and managing test servers for various web frameworks (e.g., Axum, Warp, actix-web). It allows the TestClient to work with any server implementation in a framework-agnostic way.
§Associated Types
Error
: Error type that can occur during server operations
§Required Methods
launch
: Starts the server with the provided TcpListener
§Optional Methods
is_healthy
: Checks if the server is ready to accept requestsconfig
: Provides configuration for the test framework
§Example
use clawspec_core::test_client::{TestServer, TestServerConfig, HealthStatus};
use std::net::TcpListener;
use std::time::Duration;
#[derive(Debug)]
struct ServerError;
impl std::fmt::Display for ServerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Server error")
}
}
impl std::error::Error for ServerError {}
#[derive(Debug)]
struct MyTestServer;
impl TestServer for MyTestServer {
type Error = ServerError;
async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
// Convert to non-blocking for tokio compatibility
listener.set_nonblocking(true).map_err(|_| ServerError)?;
let tokio_listener = tokio::net::TcpListener::from_std(listener)
.map_err(|_| ServerError)?;
// Start your web server here
// For example, with axum:
// axum::serve(tokio_listener, app).await.map_err(|_| ServerError)?;
Ok(())
}
async fn is_healthy(&self, client: &mut clawspec_core::ApiClient) -> Result<HealthStatus, Self::Error> {
// Check if server is ready by making a health check request
match client.get("/health").unwrap().await {
Ok(_) => Ok(HealthStatus::Healthy),
Err(_) => Ok(HealthStatus::Unhealthy),
}
}
fn config(&self) -> TestServerConfig {
TestServerConfig {
api_client: Some(
clawspec_core::ApiClient::builder()
.with_host("localhost")
.with_base_path("/api").unwrap()
),
min_backoff_delay: Duration::from_millis(10),
max_backoff_delay: Duration::from_secs(1),
backoff_jitter: true,
max_retry_attempts: 10,
}
}
}
§Framework Integration
§Framework Integration Example
use clawspec_core::test_client::{TestServer, TestServerConfig};
use std::net::TcpListener;
#[derive(Debug)]
struct WebFrameworkTestServer {
// Your web framework's app/router would go here
// For example: app: axum::Router, or app: warp::Filter, etc.
}
impl WebFrameworkTestServer {
fn new(/* app: YourApp */) -> Self {
Self { /* app */ }
}
}
impl TestServer for WebFrameworkTestServer {
type Error = std::io::Error; // or your custom error type
async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
listener.set_nonblocking(true)?;
let tokio_listener = tokio::net::TcpListener::from_std(listener)?;
// Start your web framework here:
// For Axum: axum::serve(tokio_listener, self.app.clone()).await?;
// For Warp: warp::serve(self.app.clone()).run_async(tokio_listener).await;
// For actix-web: HttpServer::new(|| self.app.clone()).listen(tokio_listener)?.run().await?;
Ok(())
}
}
§Health Checking
The is_healthy
method allows implementing custom health check logic:
- Return
Ok(HealthStatus::Healthy)
if the server is ready to accept requests - Return
Ok(HealthStatus::Unhealthy)
if the server is not ready - Return
Ok(HealthStatus::Uncheckable)
to use the default TCP connection test - Return
Err(Self::Error)
if an error occurs during health checking
The TestClient will wait for the server to become healthy before returning success.
Health check status returned by the is_healthy
method.
This enum provides more explicit control over health checking behavior
compared to the previous Option<bool>
approach.
Variants§
Healthy
Server is healthy and ready to accept requests.
Unhealthy
Server is not healthy or not ready yet.
Uncheckable
Use the default TCP connection test to determine health.
This is equivalent to the previous None
return value.
Trait Implementations§
Source§impl Clone for HealthStatus
impl Clone for HealthStatus
Source§fn clone(&self) -> HealthStatus
fn clone(&self) -> HealthStatus
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for HealthStatus
impl Debug for HealthStatus
Source§impl PartialEq for HealthStatus
impl PartialEq for HealthStatus
impl Copy for HealthStatus
impl Eq for HealthStatus
impl StructuralPartialEq for HealthStatus
Auto Trait Implementations§
impl Freeze for HealthStatus
impl RefUnwindSafe for HealthStatus
impl Send for HealthStatus
impl Sync for HealthStatus
impl Unpin for HealthStatus
impl UnwindSafe for HealthStatus
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.