Skip to content

Commit 4da6448

Browse files
dianpopaalxiord
authored andcommitted
api_server: some more unit tests for http_service
Signed-off-by: Diana Popa <[email protected]>
1 parent fbc0e9e commit 4da6448

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

api_server/src/http_service.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,18 @@ mod tests {
892892
}
893893

894894
// Error cases
895+
// Test the case where action already exists.
896+
assert!(
897+
parse_actions_req(
898+
&path_tokens,
899+
&path,
900+
Method::Put,
901+
&id_from_path,
902+
&body,
903+
&mut action_map
904+
).is_err()
905+
);
906+
895907
assert!(
896908
parse_actions_req(
897909
&path_tokens,
@@ -1033,6 +1045,81 @@ mod tests {
10331045
&body
10341046
).is_err()
10351047
);
1048+
1049+
// Test case where id from path is different.
1050+
assert!(
1051+
parse_drives_req(
1052+
&"/foo/bar"[1..].split_terminator('/').collect(),
1053+
&"/foo/bar",
1054+
Method::Put,
1055+
&Some("barr"),
1056+
&body
1057+
).is_err()
1058+
);
1059+
1060+
// Deserializing to a DriveDescription should fail when mandatory fields are missing.
1061+
let json = "{
1062+
\"drive_id\": \"bar\",
1063+
\"path_on_host\": \"/foo/bar\",
1064+
\"statee\": \"Attached\",
1065+
\"is_root_device\": true,
1066+
\"permissions\": \"ro\"
1067+
}";
1068+
let body: Chunk = Chunk::from(json);
1069+
assert!(
1070+
parse_drives_req(
1071+
&"/foo/bar"[1..].split_terminator('/').collect(),
1072+
&"/foo/bar",
1073+
Method::Put,
1074+
&Some("bar"),
1075+
&body
1076+
).is_err()
1077+
);
1078+
}
1079+
1080+
#[test]
1081+
fn test_parse_logger_source_req() {
1082+
let path = "/foo";
1083+
let path_tokens: Vec<&str> = path[1..].split_terminator('/').collect();
1084+
let json = "{
1085+
\"path\": \"tmp\",
1086+
\"level\": \"Info\",
1087+
\"show_level\": true,
1088+
\"show_log_origin\": true
1089+
}";
1090+
let body: Chunk = Chunk::from(json);
1091+
1092+
// GET
1093+
match parse_logger_req(&path_tokens, &path, Method::Get, &body) {
1094+
Ok(pr_dummy) => assert!(pr_dummy.eq(&ParsedRequest::Dummy)),
1095+
_ => assert!(false),
1096+
}
1097+
1098+
// PUT
1099+
let logger_body = serde_json::from_slice::<request::APILoggerDescription>(&body)
1100+
.expect("deserialization failed");
1101+
match parse_logger_req(&path_tokens, &path, Method::Put, &body) {
1102+
Ok(pr) => {
1103+
let (sender, receiver) = oneshot::channel();
1104+
assert!(pr.eq(&ParsedRequest::Sync(
1105+
SyncRequest::PutLogger(logger_body, sender),
1106+
receiver,
1107+
)));
1108+
}
1109+
_ => assert!(false),
1110+
}
1111+
1112+
// Error cases
1113+
assert!(parse_logger_req(&path_tokens, &path, Method::Put, &Chunk::from("foo")).is_err());
1114+
1115+
assert!(
1116+
parse_logger_req(
1117+
&"/foo/bar"[1..].split_terminator('/').collect(),
1118+
&"/foo/bar",
1119+
Method::Put,
1120+
&Chunk::from("foo")
1121+
).is_err()
1122+
);
10361123
}
10371124

10381125
#[test]
@@ -1155,6 +1242,16 @@ mod tests {
11551242
}
11561243

11571244
// Error cases
1245+
assert!(
1246+
parse_netif_req(
1247+
&"/foo/bar"[1..].split_terminator('/').collect(),
1248+
&"/foo/bar",
1249+
Method::Put,
1250+
&Some("barr"),
1251+
&body
1252+
).is_err()
1253+
);
1254+
11581255
assert!(
11591256
parse_netif_req(
11601257
&"/foo/bar"[1..].split_terminator('/').collect(),

0 commit comments

Comments
 (0)