dufs/tests/utils.rs

70 lines
1.9 KiB
Rust
Raw Normal View History

use indexmap::IndexSet;
2022-06-12 03:43:50 +03:00
use serde_json::Value;
#[macro_export]
macro_rules! assert_resp_paths {
2022-06-12 03:43:50 +03:00
($resp:ident) => {
assert_resp_paths!($resp, self::fixtures::FILES)
2022-06-12 03:43:50 +03:00
};
($resp:ident, $files:expr) => {
assert_eq!($resp.status(), 200);
let body = $resp.text()?;
2022-08-03 03:51:12 +03:00
let paths = self::utils::retrieve_index_paths(&body);
2022-06-12 03:43:50 +03:00
assert!(!paths.is_empty());
for file in $files {
assert!(paths.contains(&file.to_string()));
}
};
}
#[macro_export]
macro_rules! fetch {
($method:literal, $url:expr) => {
reqwest::blocking::Client::new().request(hyper::Method::from_bytes($method)?, $url)
};
}
#[allow(dead_code)]
pub fn retrieve_index_paths(content: &str) -> IndexSet<String> {
let value = retrive_json(content).unwrap();
2022-06-12 03:43:50 +03:00
let paths = value
.get("paths")
.unwrap()
.as_array()
.unwrap()
2022-06-12 03:43:50 +03:00
.iter()
.flat_map(|v| {
let name = v.get("name")?.as_str()?;
let path_type = v.get("path_type")?.as_str()?;
if path_type.ends_with("Dir") {
Some(format!("{name}/"))
2022-06-12 03:43:50 +03:00
} else {
Some(name.to_owned())
}
})
.collect();
paths
}
#[allow(dead_code)]
pub fn retrive_edit_file(content: &str) -> Option<bool> {
let value = retrive_json(content)?;
let value = value.get("editable").unwrap();
Some(value.as_bool().unwrap())
}
#[allow(dead_code)]
pub fn encode_uri(v: &str) -> String {
let parts: Vec<_> = v.split('/').map(urlencoding::encode).collect();
parts.join("/")
}
#[allow(dead_code)]
pub fn retrive_json(content: &str) -> Option<Value> {
let lines: Vec<&str> = content.lines().collect();
let line = lines.iter().find(|v| v.contains("DATA ="))?;
let line_col = line.find("DATA =").unwrap() + 6;
let value: Value = line[line_col..].parse().unwrap();
Some(value)
2022-06-12 03:43:50 +03:00
}