test_app.rs 1.38 KB
Newer Older
1
2
3
4
5
use axum::Router;
use reqwest::Client;
use sglang_router_rs::{
    config::RouterConfig,
    routers::RouterTrait,
6
    server::{build_app, AppContext, AppState},
7
8
9
10
};
use std::sync::Arc;

/// Create a test Axum application using the actual server's build_app function
11
#[allow(dead_code)]
12
13
14
15
16
pub fn create_test_app(
    router: Arc<dyn RouterTrait>,
    client: Client,
    router_config: &RouterConfig,
) -> Router {
17
18
19
20
21
    // Create AppContext
    let app_context = Arc::new(AppContext::new(
        router_config.clone(),
        client,
        router_config.max_concurrent_requests,
22
        router_config.rate_limit_tokens_per_second,
23
24
25
    ));

    // Create AppState with the test router and context
26
27
    let app_state = Arc::new(AppState {
        router,
28
        context: app_context,
29
        concurrency_queue_tx: None, // No queue for tests
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    });

    // Configure request ID headers (use defaults if not specified)
    let request_id_headers = router_config.request_id_headers.clone().unwrap_or_else(|| {
        vec![
            "x-request-id".to_string(),
            "x-correlation-id".to_string(),
            "x-trace-id".to_string(),
            "request-id".to_string(),
        ]
    });

    // Use the actual server's build_app function
    build_app(
        app_state,
        router_config.max_payload_size,
        request_id_headers,
        router_config.cors_allowed_origins.clone(),
    )
}