Debian Codegraph @

src/check/error.rs

raw
1 use std::any::TypeId;
2
3 use crate::response::error::Rfc9083Error;
4
5 use super::{Check, CheckParams, Checks, GetChecks, GetGroupChecks};
6
7 impl GetChecks for Rfc9083Error {
8 fn get_checks(&self, index: Option<usize>, params: CheckParams) -> super::Checks {
9 let sub_checks = {
10 GetGroupChecks::get_group_checks(&self.common, params.from_parent(TypeId::of::<Self>()))
11 };
12
13 let mut items = vec![];
14
15 if self.error_code.is_string() {
16 items.push(Check::ErrorCodeIsString.check_item());
17 }
18
19 if let Some(title) = &self.title
20 && (title.is_number() || title.is_bool())
21 {
22 items.push(Check::TitleIsNotString.check_item());
23 }
24
25 if let Some(desc) = &self.description
26 && desc.is_string()
27 {
28 items.push(Check::DescriptionIsString.check_item());
29 }
30
31 Checks {
32 rdap_struct: super::RdapStructure::Error,
33 index,
34 items,
35 sub_checks,
36 }
37 }
38 }
39
40 #[cfg(test)]
41 mod tests {
42 use crate::check::{Check, CheckParams, GetChecks, contains_check};
43 use crate::response::{RdapResponse, ToResponse, error::Rfc9083Error};
44
45 #[test]
46 fn check_error_with_string_error_code() {
47 // GIVEN
48 let json = r#"{"errorCode": "404"}"#;
49 let error = serde_json::from_str::<Rfc9083Error>(json).expect("parsing JSON");
50 let rdap = error.to_response();
51
52 // WHEN
53 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
54
55 // THEN
56 assert!(contains_check(Check::ErrorCodeIsString, &checks));
57 }
58
59 #[test]
60 fn check_error_with_number_error_code() {
61 // GIVEN
62 let json = r#"{"errorCode": 404}"#;
63 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
64
65 // WHEN
66 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
67
68 // THEN
69 assert!(!contains_check(Check::ErrorCodeIsString, &checks));
70 }
71
72 #[test]
73 fn check_error_with_number_title() {
74 // GIVEN
75 let json = r#"{"errorCode": 404, "title": 123}"#;
76 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
77
78 // WHEN
79 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
80
81 // THEN
82 assert!(contains_check(Check::TitleIsNotString, &checks));
83 }
84
85 #[test]
86 fn check_error_with_bool_title() {
87 // GIVEN
88 let json = r#"{"errorCode": 404, "title": true}"#;
89 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
90
91 // WHEN
92 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
93
94 // THEN
95 assert!(contains_check(Check::TitleIsNotString, &checks));
96 }
97
98 #[test]
99 fn check_error_with_string_title() {
100 // GIVEN
101 let json = r#"{"errorCode": 404, "title": "Not Found"}"#;
102 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
103
104 // WHEN
105 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
106
107 // THEN
108 assert!(!contains_check(Check::TitleIsNotString, &checks));
109 }
110
111 #[test]
112 fn check_error_with_string_description() {
113 // GIVEN
114 let json = r#"{"errorCode": 404, "description": "Not found"}"#;
115 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
116
117 // WHEN
118 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
119
120 // THEN
121 assert!(contains_check(Check::DescriptionIsString, &checks));
122 }
123
124 #[test]
125 fn check_error_with_array_description() {
126 // GIVEN
127 let json = r#"{"errorCode": 404, "description": ["Not found"]}"#;
128 let rdap = serde_json::from_str::<RdapResponse>(json).expect("parsing JSON");
129
130 // WHEN
131 let checks = rdap.get_checks(None, CheckParams::for_rdap(&rdap));
132
133 // THEN
134 assert!(!contains_check(Check::DescriptionIsString, &checks));
135 }
136 }