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 "Redirection responses",
8 ResponsesRedirectionCodes,
9 MultipleChoices => (300, "Multiple Choices", "The request has more than one possible response. The user-agent or user should choose one of them. There is no standardized way of choosing one of the responses, but HTML links to the possibilities are recommended so the user can pick manually", 300, "Multiple Choices"),
10 MovedPermanently => (301, "Moved Permanently", "The resource has been permanently moved to a new URI. Future requests should use the new URI. This status code is typically used for URL redirection", 301, "Moved Permanently"),
11 Found => (302, "Found", "The resource is temporarily available at a different URI. The client should continue using the original URI for future requests. This status code is often used for URL redirection", 302, "Found"),
12 SeeOther => (303, "See Other", "The response to the request can be found under another URI, and the client should use GET to retrieve it. This status code is used to direct the client to retrieve the resource from a different URI", 303, "See Other"),
13 NotModified => (304, "Not Modified", "The resource has not been modified since the version specified in the request headers. This status code is used for caching purposes to reduce unnecessary network traffic", 304, "Not Modified"),
14 UseProxy => (305, "Use Proxy", "The requested resource must be accessed through a specified proxy. This status code is used to inform the client that it should use a proxy server to access the resource", 305, "Use Proxy"),
15 SwitchProxy => (306, "Unused", "Originally 'Switch Proxy', no longer used.", 306, "Switch Proxy"),
16 TemporaryRedirect => (307, "Temporary Redirect", "The resource is temporarily located at a different URI. The client should use the same method to access it. This status code is used for temporary URL redirection", 307, "Temporary Redirect"),
17 PermanentRedirect => (308, "Permanent Redirect", "The resource has been permanently moved to a new URI. The client should update its references. This status code is used for permanent URL redirection", 308, "Permanent Redirect"),
18 TooManyRedirects => (300, "Multiple Choices", "The client has been redirected too many times, possibly causing a redirection loop. This status code is used to prevent infinite redirection loops", 310, "Too Many Redirects"),
19 RedirectMethod => (300, "Multiple Choices", "The client should use a different method to access the resource. This status code is used to inform the client that it should use a different HTTP method, such as GET or POST", 311, "Redirect Method"),
20 Unassigned => (300, "Multiple Choices", "This code is currently unassigned and reserved for future use. It may be used for a new feature or status code in the future", 312, "Unassigned"),
21 MovedPermanentlyRedirected => (300, "Multiple Choices", "The requested resource has been permanently moved to a new URI, but the client should continue to use the original URI. This status code is used for special cases of permanent redirection", 321, "Moved Permanently Redirected"),
22 MovedTemporarilyRedirected => (300, "Multiple Choices", "The requested resource is temporarily available at a new URI but the client should not update its original URI. This status code is used for special cases of temporary redirection", 322, "Moved Temporarily Redirected"),
23 SeeOtherRedirected => (300, "Multiple Choices", "The requested resource can be accessed at a different URI using the GET method. This status code is used to direct the client to retrieve the resource from a different URI using GET", 323, "See Other Redirected"),
24 NotModifiedRedirected => (300, "Multiple Choices", "The requested resource has not been modified and can be retrieved from the cache. This status code is used for caching purposes to reduce unnecessary network traffic", 324, "Not Modified Redirected"),
25 UseProxyRedirected => (300, "Multiple Choices", "The resource must be accessed through a proxy, and the proxy details are provided. This status code is used to inform the client that it should use a proxy server to access the resource", 325, "Use Proxy Redirected"),
26 UnusedRedirected => (300, "Multiple Choices", "This status code is reserved and not used anymore. It was previously used for a proposed feature that was never implemented", 326, "Unused Redirected"),
27 TemporaryRedirectRedirected => (300, "Multiple Choices", "The requested resource is temporarily located at a new URI. The client should not update its reference. This status code is used for special cases of temporary redirection", 327, "Temporary Redirect Redirected"),
28 PermanentRedirected => (300, "Multiple Choices", "The resource has been permanently moved to a new URI, and future requests should use the new URI. This status code is used for special cases of permanent redirection", 328, "Permanent Redirected"),
29 TooManyRedirectsRedirected => (300, "Multiple Choices", "The client has been redirected too many times during a redirection loop. This status code is used to prevent infinite redirection loops", 329, "Too Many Redirects Redirected"),
30 RedirectMethodRedirected => (300, "Multiple Choices", "The redirection requires the client to use a different request method. This status code is used to inform the client that it should use a different HTTP method, such as GET or POST", 330, "Redirect Method Redirected"),
31 UserNameOkPasswordNeeded => (300, "Multiple Choices", "The username is valid, but the client must provide a password to proceed. This status code is used for authentication purposes", 331, "User Name Ok Password Needed"),
32 NoNeedAccountForLogin => (300, "Multiple Choices", "The requested resource does not require a user account for access. This status code is used to inform the client that no login is necessary", 332, "No Need Account For Login"),
33 SessionKeyNotPresentInHeader => (300, "Multiple Choices", "The request is missing a session key in the header. This status code is used for session management purposes", 333, "Session Key Not Present In Header"),
34 SessionKeyPresentAndNotDecryptableParsable => (300, "Multiple Choices", "The session key provided in the request cannot be decrypted or parsed. This status code is used for session management purposes", 334, "Session Key Present And Not Decryptable Parsable"),
35 ServerIsUnwillingToProcessTheRequest => (300, "Multiple Choices", "The server refuses to process the request, often due to policy restrictions. This status code is used to inform the client that the server is unwilling to process the request", 335, "Server Is Unwilling To Process The Request"),
36 ChallengeResponseAuthenticationOk => (300, "Multiple Choices", "Challenge-response authentication was successfully completed. This status code is used to inform the client that authentication was successful", 336, "Challenge Response Authentication Ok"),
37 ChallengeResponseAuthenticationFailed => (300, "Multiple Choices", "Challenge-response authentication failed due to invalid credentials or other issues. This status code is used to inform the client that authentication failed", 337, "Challenge Response Authentication Failed"),
38 LengthRequired => (300, "Multiple Choices", "The request did not specify the length of its content, which is required by the server. This status code is used to inform the client that the length is required", 342, "Length Required"),
39 PreconditionFailed => (300, "Multiple Choices", "The server does not meet the preconditions set by the client in its request. This status code is used to inform the client that the preconditions failed", 343, "Precondition Failed"),
40 RequestEntityTooLarge => (300, "Multiple Choices", "The request is larger than the server is willing or able to process. This status code is used to inform the client that the request entity is too large", 344, "Request Entity Too Large"),
41 UnsupportedMediaType => (300, "Multiple Choices", "The media type of the request is not supported by the server. This status code is used to inform the client that the media type is unsupported", 346, "Unsupported Media Type"),
42 RequestedRangeNotSatisfiable => (300, "Multiple Choices", "The server cannot supply the portion of the file requested by the client. This status code is used to inform the client that the requested range is not satisfiable", 347, "Requested Range Not Satisfiable"),
43 ExpectationFailed => (300, "Multiple Choices", "The server cannot meet the requirements specified in the Expect header of the request. This status code is used to inform the client that the expectation failed", 348, "Expectation Failed"),
44 ImATeapot => (300, "Multiple Choices", "A humorous response indicating the server is a teapot and refuses to brew coffee. This status code is used as an April Fools' joke", 349, "I'm A Teapot"),
45 ErrorAccessingURL => (300, "Multiple Choices", "The server encountered an error while attempting to access the specified URL. This status code is used to inform the client that there was an error accessing the URL", 350, "Error Accessing URL"),
46 TriggerNotFound => (300, "Multiple Choices", "The requested redirection trigger could not be found on the server. This status code is used to inform the client that the trigger was not found", 351, "Trigger Not Found"),
47 AccessDenied => (300, "Multiple Choices", "The server refuses to fulfill the request due to access restrictions. This status code is used to inform the client that access is denied", 352, "Access Denied"),
48 ConditionFailed => (300, "Multiple Choices", "A condition required to complete the redirection was not satisfied. This status code is used to inform the client that the condition failed", 353, "Condition Failed"),
49 MandatoryParameterIsNull => (300, "Multiple Choices", "A required parameter for the request is missing or null. This status code is used to inform the client that a mandatory parameter is null", 354, "Mandatory Parameter Is Null"),
50 TheParameterDoesNotExist => (300, "Multiple Choices", "A parameter specified in the request does not exist. This status code is used to inform the client that the parameter does not exist", 355, "The Parameter Does Not Exist"),
51 DataBLOBShouldNotBeNullForPostMethod => (300, "Multiple Choices", "The data payload for a POST request must not be null. This status code is used to inform the client that the data BLOB should not be null for POST method", 356, "Data BLOB Should Not Be Null For Post Method"),
52}
53
54#[cfg(test)]
55mod tests {
56 use crate::helpers::unified_tuple_helper::UnifiedTuple;
57 use crate::responses::ResponsesRedirectionCodes;
58 use crate::traits::tuple_traits::IntoTwoFieldsTuple;
59 use serde_json::json;
60 use serde_json::to_value;
61
62 #[test]
63 fn test_redirection_codes_get_code() {
64 assert_eq!(ResponsesRedirectionCodes::MultipleChoices.get_code(), 300);
65 assert_eq!(ResponsesRedirectionCodes::MovedPermanently.get_code(), 301);
66 assert_eq!(ResponsesRedirectionCodes::SeeOther.get_code(), 303);
67 assert_eq!(ResponsesRedirectionCodes::TemporaryRedirect.get_code(), 307);
68 }
69
70 #[test]
71 fn test_redirection_codes_from_u16() {
72 assert_eq!(
73 ResponsesRedirectionCodes::from_u16(301),
74 Some(ResponsesRedirectionCodes::MovedPermanently)
75 );
76 assert_eq!(
77 ResponsesRedirectionCodes::from_u16(303),
78 Some(ResponsesRedirectionCodes::SeeOther)
79 );
80 assert_eq!(
81 ResponsesRedirectionCodes::from_u16(321),
82 Some(ResponsesRedirectionCodes::MovedPermanentlyRedirected)
83 );
84 assert_eq!(ResponsesRedirectionCodes::from_u16(9999), None);
85 }
86
87 #[test]
88 fn test_server_is_unwilling_to_process_the_request_codes_as_tuple() {
89 let code = ResponsesRedirectionCodes::ServerIsUnwillingToProcessTheRequest;
90 let tuple = UnifiedTuple {
91 standard_code: 300,
92 standard_name: "Multiple Choices",
93 unified_description: "The server refuses to process the request, often due to policy restrictions. This status code is used to inform the client that the server is unwilling to process the request",
94 internal_code: Some(335),
95 internal_name: Some("Server Is Unwilling To Process The Request")
96 };
97 let code_as_tuple = code.as_tuple();
98 assert_eq!(code_as_tuple, tuple);
99 }
100
101 #[test]
102 fn test_redirection_codes_as_json() {
103 let response_code = ResponsesRedirectionCodes::UserNameOkPasswordNeeded;
104 let json_result = response_code.as_json();
105 let expected_json = json!({
106 "type": "Redirection responses",
107 "details": {
108 "standard http code": {
109 "code": 300,
110 "name": "Multiple Choices"
111 },
112 "description": "The username is valid, but the client must provide a password to proceed. This status code is used for authentication purposes",
113 "internal http code": {
114 "code": 331,
115 "name": "User Name Ok Password Needed"
116 }
117 }
118 });
119
120 assert_eq!(json_result, expected_json);
121 }
122
123 #[test]
124 fn test_temporary_redirect_codes_into_two_fields_tuple() {
125 let response_code = ResponsesRedirectionCodes::TemporaryRedirect;
126 let tuple = response_code.into_two_fields_tuple();
127 let json_result = to_value(&tuple).unwrap();
128
129 let expected_json = json!({
130 "code": 307,
131 "name": "Temporary Redirect"
132 });
133
134 assert_eq!(json_result, expected_json);
135 }
136
137 #[test]
138 fn test_multiples_choices_standard_codes() {
139 assert_eq!(
141 ResponsesRedirectionCodes::from_u16(355),
142 Some(ResponsesRedirectionCodes::TheParameterDoesNotExist)
143 );
144 assert_eq!(
145 ResponsesRedirectionCodes::from_u16(356),
146 Some(ResponsesRedirectionCodes::DataBLOBShouldNotBeNullForPostMethod)
147 );
148 }
149}