simbld_http/
lib.rs

1/// Provides response codes, helpers, and utility functions for HTTP response management.
2#[macro_use]
3pub mod helpers;
4
5pub mod mocks;
6pub mod responses;
7pub mod traits;
8pub mod utils;
9
10// Public exports for helpers
11pub use helpers::auth_middleware::AuthMiddleware;
12pub use helpers::generate_responses_functions;
13pub use helpers::http_interceptor_helper::HttpInterceptor;
14pub use helpers::unified_middleware_helper::UnifiedMiddleware;
15
16// Public exports for mocks
17pub use mocks::mock_responses::MockResponses;
18
19// External crates re-exported for convenience
20pub use serde_json::{json, Value};
21
22// Public exports for response modules
23pub use responses::ResponsesClientCodes;
24pub use responses::ResponsesCrawlerCodes;
25pub use responses::ResponsesInformationalCodes;
26pub use responses::ResponsesLocalApiCodes;
27pub use responses::ResponsesRedirectionCodes;
28pub use responses::ResponsesServerCodes;
29pub use responses::ResponsesServiceCodes;
30pub use responses::ResponsesSuccessCodes;
31
32// Module for tests
33#[cfg(test)]
34mod tests {
35    use crate::helpers::http_code_helper::HttpCode;
36    use crate::responses::ResponsesCrawlerCodes;
37    use crate::ResponsesSuccessCodes;
38    use serde_json::json;
39
40    /// Test `get_code` method for `ResponsesCrawlerCodes`.
41    #[test]
42    fn test_crawler_codes_get_code() {
43        assert_eq!(ResponsesCrawlerCodes::ParsingErrorHeader.get_code(), 400);
44    }
45
46    /// Test `from_u16` method for `ResponsesCrawlerCodes`.
47    #[test]
48    fn test_crawler_codes_from_u16() {
49        assert_eq!(
50            ResponsesCrawlerCodes::from_u16(700),
51            Some(ResponsesCrawlerCodes::ParsingErrorUnfinishedHeader)
52        );
53    }
54
55    /// Test `as_tuple` method for `ResponsesCrawlerCodes`.
56    #[test]
57    fn test_crawler_codes_as_tuple() {
58        let code = ResponsesCrawlerCodes::InvalidURL;
59        let http_code = code.to_http_code();
60        assert_eq!(
61            http_code,
62            HttpCode {
63                standard_code: 400,
64                standard_name: "Bad Request",
65                unified_description: "Invalid URL encountered by crawler.",
66                internal_code: Some(786),
67                internal_name: Some("Invalid URL")
68            }
69        );
70    }
71
72    /// Test `as_json` method for unified tuple format with standard code equals internal code.
73    #[test]
74    fn test_ok_codes_as_json() {
75        let code = ResponsesSuccessCodes::Ok;
76        let json_result = code.as_json();
77
78        let expected = json!({
79            "type": "Success responses",
80            "details": {
81                "standard http code": {
82                    "code": 200,
83                    "name": "OK"
84                },
85            "description": "Request processed successfully. Response will depend on the request method used, and the result will be either a representation of the requested resource or an empty response",
86                "internal http code": {
87                    "code": null,
88                    "name": null,
89        }}});
90
91        assert_eq!(json_result, expected);
92    }
93
94    /// Test `as_json` method for unified tuple format with standard code differs internal code.
95    #[test]
96    fn test_crawler_codes_as_json() {
97        let code = ResponsesCrawlerCodes::RobotsTemporarilyUnavailable;
98        let json_result = code.as_json();
99
100        let expected = json!({
101            "type": "Crawler responses",
102            "details": {
103            "standard http code": {
104                "code": 503,
105                "name": "Service Unavailable"
106            },
107            "description": "Robots temporarily unavailable.",
108            "internal http code": {
109                "code": 741,
110                "name": "Robots Temporarily Unavailable"
111            },
112        }});
113
114        assert_eq!(json_result, expected);
115    }
116}