Skip to main content

redact_api/
routes.rs

1// Copyright 2026 Censgate LLC.
2// Licensed under the Apache License, Version 2.0. See the LICENSE file
3// in the project root for license information.
4
5use crate::handlers::{analyze, anonymize, health, AppState};
6use axum::{
7    routing::{get, post},
8    Router,
9};
10
11/// Create the application router with all routes
12pub fn create_router(state: AppState) -> Router {
13    Router::new()
14        .route("/health", get(health))
15        .route("/api/v1/analyze", post(analyze))
16        .route("/api/v1/anonymize", post(anonymize))
17        .with_state(state)
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use axum::{
24        body::Body,
25        http::{Request, StatusCode},
26    };
27    use redact_core::AnalyzerEngine;
28    use std::sync::Arc;
29    use tower::util::ServiceExt;
30
31    #[tokio::test]
32    async fn test_health_route() {
33        let state = AppState {
34            engine: Arc::new(AnalyzerEngine::new()),
35        };
36        let app = create_router(state);
37
38        let response = app
39            .oneshot(
40                Request::builder()
41                    .uri("/health")
42                    .body(Body::empty())
43                    .unwrap(),
44            )
45            .await
46            .unwrap();
47
48        assert_eq!(response.status(), StatusCode::OK);
49    }
50
51    #[tokio::test]
52    async fn test_analyze_route() {
53        let state = AppState {
54            engine: Arc::new(AnalyzerEngine::new()),
55        };
56        let app = create_router(state);
57
58        let body = r#"{"text":"john@example.com","language":"en"}"#;
59
60        let response = app
61            .oneshot(
62                Request::builder()
63                    .method("POST")
64                    .uri("/api/v1/analyze")
65                    .header("content-type", "application/json")
66                    .body(Body::from(body))
67                    .unwrap(),
68            )
69            .await
70            .unwrap();
71
72        assert_eq!(response.status(), StatusCode::OK);
73    }
74}