Skip to main content

actix_web_schema/
lib.rs

1pub use actix_web_schema_macro::*;
2
3#[cfg(test)]
4mod tests {
5    use super::*;
6
7    /// 测试服务
8    #[service]
9    pub trait Hello {
10        /// 一个测试接口
11        #[get("/api/v1/hello")]
12        async fn hello() -> Result<HelloResponse, Box<dyn std::error::Error>>;
13
14        /// 登录接口
15        #[post("/api/v1/login")]
16        async fn login(request: actix_web::web::Json<LoingRequest>) -> Result<LoingResponse, Box<dyn std::error::Error>>;
17    }
18
19    /// Hello的响应
20    #[response]
21    pub struct HelloResponse {
22        pub message: &'static str,
23    }
24
25    /// 登录接口的请求
26    #[request]
27    pub struct LoingRequest {
28        pub username: String,
29        pub password: String,
30    }
31
32    #[response]
33    pub struct LoingResponse {
34        pub token: String,
35    }
36
37    #[response(raw)]
38    pub struct RawResponse {
39        pub code: i32,
40        pub message: String,
41    }
42
43    impl Hello for HelloService {
44        async fn hello() -> Result<HelloResponse, Box<dyn std::error::Error>> {
45            Err("Just for test".into())
46        }
47
48        async fn login(_request: actix_web::web::Json<LoingRequest>) -> Result<LoingResponse, Box<dyn std::error::Error>> {
49            Err("Just for test".into())
50        }
51    }
52
53    #[actix_web::test]
54    async fn test_response_wrapped_format() {
55        use actix_web::Responder;
56
57        let req = actix_web::test::TestRequest::default().to_http_request();
58        let response = HelloResponse {
59            message: "hello world",
60        };
61
62        let resp = response.respond_to(&req);
63
64        assert_eq!(resp.status(), actix_web::http::StatusCode::OK);
65
66        let body_bytes = actix_web::body::to_bytes(resp.into_body()).await.unwrap();
67        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
68
69        assert_eq!(json["code"], 0);
70        assert_eq!(json["data"]["message"], "hello world");
71    }
72
73    #[actix_web::test]
74    async fn test_response_raw_format() {
75        use actix_web::Responder;
76
77        let req = actix_web::test::TestRequest::default().to_http_request();
78        let response = RawResponse {
79            code: 200,
80            message: "success".to_string(),
81        };
82
83        let resp = response.respond_to(&req);
84
85        assert_eq!(resp.status(), actix_web::http::StatusCode::OK);
86
87        let body_bytes = actix_web::body::to_bytes(resp.into_body()).await.unwrap();
88        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
89
90        // Raw format should not be wrapped in {"code": 0, "data": ...}
91        assert_eq!(json["code"], 200);
92        assert_eq!(json["message"], "success");
93        assert!(json.get("data").is_none());
94    }
95
96    #[actix_web::test]
97    async fn test_response_login_response() {
98        use actix_web::Responder;
99
100        let req = actix_web::test::TestRequest::default().to_http_request();
101        let response = LoingResponse {
102            token: "test_token_123".to_string(),
103        };
104
105        let resp = response.respond_to(&req);
106
107        assert_eq!(resp.status(), actix_web::http::StatusCode::OK);
108
109        let body_bytes = actix_web::body::to_bytes(resp.into_body()).await.unwrap();
110        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
111
112        assert_eq!(json["code"], 0);
113        assert_eq!(json["data"]["token"], "test_token_123");
114    }
115}