simbld_http/responses/
informational.rs

1use crate::generate_responses_functions;
2use crate::responses::CustomResponse;
3use crate::traits::get_code_trait::GetCode;
4use strum_macros::EnumIter;
5
6generate_responses_functions! {
7"Informational responses",
8  ResponsesInformationalCodes,
9  ContinueRequest => (100, "Continue", "The server has received the initial part of the request, the headers, and asks the client to continue request, proceed to send the body of the request, a POST request", 100, "Continue Request"),
10  SwitchingProtocols => (101, "Switching Protocols", "The server is complying with a request to switch protocols, used in WebSocket connections", 101, "Switching Protocols"),
11  Processing => (102, "Processing", "Indicates the server is processing the request but has not yet finished, used to prevent timeout errors in asynchronous operations, webdav RFC 2518", 102, "Processing"),
12  EarlyHints => (103, "Early Hints", "Experimental: The server provides preliminary hints to the client, such as preloading resources while the final response is being prepared", 103, "Early Hints"),
13  ConnectionResetByPeer => (100, "Continue", "The connection was forcibly closed by a peer, possibly due to a protocol error, a timeout, or a network issue", 104, "Connection Reset By Peer"),
14  NameNotResolved => (100, "Continue", "The server could not resolve the domain name provided in the request, indicating a DNS lookup failure, The requested hostname cannot be resolved to an IP address", 105, "Name Not Resolved"),
15  NoResponse => (100, "Continue", "The server did not provide a response, possibly due to a timeout or a connection issue, The server didn’t send any response within the timeout period. This status code is not specified in any RFCs, but it is used in some scenarios to indicate that the server closed the connection without sending any response", 106, "No Response"),
16  RetryWith => (100, "Continue", "The server indicates that the client should retry the request with appropriate changes or additional information, new or different credentials, use a different protocol or in a different location", 107, "Retry With"),
17  ResponseIsStale => (100, "Continue", "The response returned by the server is stale and should be revalidated, indicating that the cached response is outdated or expired", 108, "Response Is Stale"),
18  RevalidationFailed => (100, "Continue", "The server attempted to validate a cached response but failed, indicating the cached response is invalid or expired", 109, "Revalidation Failed"),
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::helpers::unified_tuple_helper::UnifiedTuple;
24    use crate::responses::ResponsesInformationalCodes;
25    use crate::traits::tuple_traits::IntoTwoFieldsTuple;
26    use serde_json::json;
27    use serde_json::to_value;
28
29    #[test]
30    fn test_to_16_switching_protocols() {
31        assert_eq!(ResponsesInformationalCodes::ContinueRequest.get_code(), 100);
32        assert_eq!(ResponsesInformationalCodes::SwitchingProtocols.get_code(), 101);
33        assert_eq!(ResponsesInformationalCodes::Processing.get_code(), 102);
34        assert_eq!(ResponsesInformationalCodes::EarlyHints.get_code(), 103);
35    }
36
37    #[test]
38    fn test_processing_codes_from_u16() {
39        assert_eq!(
40            ResponsesInformationalCodes::from_u16(102),
41            Some(ResponsesInformationalCodes::Processing)
42        );
43        assert_eq!(
44            ResponsesInformationalCodes::from_u16(103),
45            Some(ResponsesInformationalCodes::EarlyHints)
46        );
47        assert_eq!(
48            ResponsesInformationalCodes::from_u16(104),
49            Some(ResponsesInformationalCodes::ConnectionResetByPeer)
50        );
51        assert_eq!(ResponsesInformationalCodes::from_u16(9999), None);
52    }
53
54    #[test]
55    fn test_response_is_stale_codes_as_tuple() {
56        let code = ResponsesInformationalCodes::ResponseIsStale;
57        let tuple = UnifiedTuple {
58            standard_code: 100,
59            standard_name: "Continue",
60            unified_description: "The response returned by the server is stale and should be revalidated, indicating that the cached response is outdated or expired",
61            internal_code: Some(108),
62            internal_name: Option::from("Response Is Stale"),
63        };
64        let code_as_tuple = code.as_tuple();
65        assert_eq!(code_as_tuple, tuple);
66    }
67
68    #[test]
69    fn test_revalidation_failed_codes_as_json() {
70        let response_code = ResponsesInformationalCodes::RevalidationFailed;
71        let json_result = response_code.as_json();
72        let expected_json = json!({
73            "type": "Informational responses",
74            "details": {
75                "standard http code": {
76                    "code": 100,
77                    "name": "Continue"
78                },
79                "description": "The server attempted to validate a cached response but failed, indicating the cached response is invalid or expired",
80                "internal http code": {
81                    "code": 109,
82                    "name": "Revalidation Failed"
83                }
84        }});
85
86        assert_eq!(json_result, expected_json);
87    }
88
89    #[test]
90    fn test_continue_request_codes_into_two_fields_tuple() {
91        let response_code = ResponsesInformationalCodes::ContinueRequest;
92        let tuple = response_code.into_two_fields_tuple();
93        let json_result = to_value(&tuple).unwrap();
94
95        let expected_json = json!({
96            "code": 100,
97            "name": "Continue"
98        });
99
100        assert_eq!(json_result, expected_json);
101    }
102
103    #[test]
104    fn test_continue_duplicate_standard_codes() {
105        assert_eq!(
106            ResponsesInformationalCodes::from_u16(108),
107            Some(ResponsesInformationalCodes::ResponseIsStale)
108        );
109        assert_eq!(
110            ResponsesInformationalCodes::from_u16(109),
111            Some(ResponsesInformationalCodes::RevalidationFailed)
112        );
113    }
114}