mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 02:35:31 +00:00
aquatic_http: continue implementation work
This commit is contained in:
parent
76079cf66e
commit
a487347a0d
6 changed files with 96 additions and 171 deletions
|
|
@ -126,122 +126,4 @@ pub fn deserialize_info_hashes<'de, D>(
|
|||
where D: Deserializer<'de>,
|
||||
{
|
||||
Ok(deserializer.deserialize_any(InfoHashVecVisitor).unwrap_or_default())
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn info_hash_from_bytes(bytes: &[u8]) -> InfoHash {
|
||||
let mut arr = [0u8; 20];
|
||||
|
||||
assert!(bytes.len() == 20);
|
||||
|
||||
arr.copy_from_slice(&bytes[..]);
|
||||
|
||||
InfoHash(arr)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_20_bytes(){
|
||||
let input = r#""aaaabbbbccccddddeeee""#;
|
||||
|
||||
let expected = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
let observed: InfoHash = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
|
||||
let input = r#""aaaabbbbccccddddeee""#;
|
||||
let res_info_hash: Result<InfoHash, _> = serde_json::from_str(input);
|
||||
|
||||
assert!(res_info_hash.is_err());
|
||||
|
||||
let input = r#""aaaabbbbccccddddeee𝕊""#;
|
||||
let res_info_hash: Result<InfoHash, _> = serde_json::from_str(input);
|
||||
|
||||
assert!(res_info_hash.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serde_20_bytes(){
|
||||
let info_hash = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
|
||||
let out = serde_json::to_string(&info_hash).unwrap();
|
||||
let info_hash_2 = serde_json::from_str(&out).unwrap();
|
||||
|
||||
assert_eq!(info_hash, info_hash_2);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
||||
struct Test {
|
||||
#[serde(deserialize_with = "deserialize_info_hashes", default)]
|
||||
info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_vec(){
|
||||
let input = r#"{
|
||||
"info_hashes": ["aaaabbbbccccddddeeee", "aaaabbbbccccddddeeee"]
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_str(){
|
||||
let input = r#"{
|
||||
"info_hashes": "aaaabbbbccccddddeeee"
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_null(){
|
||||
let input = r#"{
|
||||
"info_hashes": null
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_missing(){
|
||||
let input = r#"{}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue