1 // Copyright (C) 2024 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 #[cfg(test)] 15 mod ut_error { 16 use super::*; 17 18 // @tc.name: ut_http_error_code_variants 19 // @tc.desc: Test HttpErrorCode variants have correct numeric values 20 // @tc.precon: NA 21 // @tc.step: 1. Verify numeric values of selected HttpErrorCode variants 22 // @tc.expect: Each variant has the expected numeric value 23 // @tc.type: FUNC 24 // @tc.require: issueNumber 25 // @tc.level: Level 1 26 #[test] ut_http_error_code_variants_001()27 fn ut_http_error_code_variants_001() { 28 assert_eq!(HttpErrorCode::HttpNoneErr as i32, 0); 29 assert_eq!(HttpErrorCode::HttpPermissionDeniedCode as i32, 201); 30 assert_eq!(HttpErrorCode::HttpParseErrorCode as i32, 401); 31 assert_eq!(HttpErrorCode::HttpErrorCodeBase as i32, 2300000); 32 assert_eq!(HttpErrorCode::HttpCouldntResolveProxy as i32, 2300005); 33 assert_eq!(HttpErrorCode::HttpUnknownOtherError as i32, 2300999); 34 } 35 36 // @tc.name: ut_http_error_code_equality 37 // @tc.desc: Test HttpErrorCode equality comparisons 38 // @tc.precon: NA 39 // @tc.step: 1. Compare equivalent and non-equivalent error codes 40 // @tc.expect: Equal codes return true, different codes return false 41 // @tc.type: FUNC 42 // @tc.require: issueNumber 43 // @tc.level: Level 1 44 #[test] ut_http_error_code_equality_001()45 fn ut_http_error_code_equality_001() { 46 let code1 = HttpErrorCode::HttpPermissionDeniedCode; 47 let code2 = HttpErrorCode::HttpPermissionDeniedCode; 48 let code3 = HttpErrorCode::HttpParseErrorCode; 49 50 assert_eq!(code1, code2); 51 assert_ne!(code1, code3); 52 } 53 54 // @tc.name: ut_http_error_code_default 55 // @tc.desc: Test HttpErrorCode default value 56 // @tc.precon: NA 57 // @tc.step: 1. Get default value of HttpErrorCode 58 // @tc.expect: Default is HttpUnknownOtherError 59 // @tc.type: FUNC 60 // @tc.require: issueNumber 61 // @tc.level: Level 0 62 #[test] ut_http_error_code_default_001()63 fn ut_http_error_code_default_001() { 64 let default_code: HttpErrorCode = Default::default(); 65 assert_eq!(default_code, HttpErrorCode::HttpUnknownOtherError); 66 } 67 68 // @tc.name: ut_http_client_error_creation 69 // @tc.desc: Test HttpClientError creation 70 // @tc.precon: NA 71 // @tc.step: 1. Create HttpClientError with specific code and message 72 // 2. Verify code and message are set correctly 73 // @tc.expect: Error contains the specified code and message 74 // @tc.type: FUNC 75 // @tc.require: issueNumber 76 // @tc.level: Level 1 77 #[test] ut_http_client_error_creation_001()78 fn ut_http_client_error_creation_001() { 79 let code = HttpErrorCode::HttpPermissionDeniedCode; 80 let msg = String::from("Test error message"); 81 let error = HttpClientError::new(code.clone(), msg.clone()); 82 83 assert_eq!(error.code(), &code); 84 assert_eq!(error.msg(), &msg); 85 } 86 87 // @tc.name: ut_http_client_error_clone 88 // @tc.desc: Test HttpClientError cloning 89 // @tc.precon: NA 90 // @tc.step: 1. Create error and clone it 91 // 2. Verify clone has same code and message 92 // @tc.expect: Cloned error is identical to original 93 // @tc.type: FUNC 94 // @tc.require: issueNumber 95 // @tc.level: Level 1 96 #[test] ut_http_client_error_clone_001()97 fn ut_http_client_error_clone_001() { 98 let original = HttpClientError::new( 99 HttpErrorCode::HttpParseErrorCode, 100 String::from("Original message") 101 ); 102 let cloned = original.clone(); 103 104 assert_eq!(original.code(), cloned.code()); 105 assert_eq!(original.msg(), cloned.msg()); 106 } 107 108 // @tc.name: ut_http_error_code_edge_cases 109 // @tc.desc: Test HttpErrorCode edge cases 110 // @tc.precon: NA 111 // @tc.step: 1. Test boundary values and special cases 112 // @tc.expect: All edge cases handled correctly 113 // @tc.type: FUNC 114 // @tc.require: issueNumber 115 // @tc.level: Level 2 116 #[test] ut_http_error_code_edge_cases_001()117 fn ut_http_error_code_edge_cases_001() { 118 // Test minimum and maximum variants 119 assert_eq!(HttpErrorCode::HttpNoneErr as i32, 0); 120 assert_eq!(HttpErrorCode::HttpUnknownOtherError as i32, 2300999); 121 122 // Test variants with and without explicit values 123 assert_eq!(HttpErrorCode::HttpUnsupportedProtocol as i32, 2300001); 124 assert_eq!(HttpErrorCode::HttpFailedInit as i32, 2300002); 125 } 126 } 127