elif_http/testing/
fixtures.rs

1//! Test fixtures and utilities
2
3use crate::{ElifRequest, ElifResponse, HttpConfig, HttpResult};
4use serde::{Deserialize, Serialize};
5
6#[derive(Deserialize, Serialize, Debug, Clone)]
7pub struct TestUser {
8    pub id: u32,
9    pub name: String,
10    pub email: String,
11}
12
13impl TestUser {
14    pub fn new(id: u32, name: &str, email: &str) -> Self {
15        Self {
16            id,
17            name: name.to_string(),
18            email: email.to_string(),
19        }
20    }
21
22    pub fn alice() -> Self {
23        Self::new(1, "Alice", "alice@example.com")
24    }
25
26    pub fn bob() -> Self {
27        Self::new(2, "Bob", "bob@example.com")
28    }
29}
30
31#[derive(Deserialize)]
32pub struct TestQuery {
33    pub limit: Option<u32>,
34    pub offset: Option<u32>,
35}
36
37/// Create a test HTTP configuration
38pub fn test_http_config() -> HttpConfig {
39    let mut config = HttpConfig::default();
40    config.request_timeout_secs = 5;
41    config.health_check_path = "/test-health".to_string();
42    config
43}
44
45/// Sample handler for testing
46pub async fn test_handler(_req: ElifRequest) -> HttpResult<ElifResponse> {
47    Ok(ElifResponse::ok().text("test response"))
48}
49
50/// Sample JSON handler for testing
51pub async fn test_json_handler(_req: ElifRequest) -> HttpResult<ElifResponse> {
52    let user = TestUser::alice();
53    ElifResponse::ok().json(&user)
54}
55
56/// Sample error handler for testing
57pub async fn test_error_handler(_req: ElifRequest) -> HttpResult<ElifResponse> {
58    Err(crate::errors::HttpError::bad_request("Test error"))
59}