better unit testing for FileID

This commit is contained in:
Jörn-Michael Miehe 2025-06-26 10:06:43 +00:00
parent 087cef5d6f
commit e0c5b5517f

View file

@ -69,16 +69,62 @@ mod tests {
use super::*;
#[test]
fn test_fileid_tryfrom_string() {
let good = "https://example.com/api/v2/alias/upload/SID123/files/tus/FID456".to_owned();
let good = FileID::try_from(good);
assert!(good.is_ok());
assert_eq!(good.unwrap().as_ref(), "FID456");
fn valid_urls_produce_expected_file_id() {
// a handful of validlooking URLs
let cases = vec![
(
"http://example.com/api/v2/alias/upload/SID123/files/tus/FID456",
"FID456",
),
(
"https://my-host:8080/api/v2/alias/upload/another-SID/files/tus/some-file-id",
"some-file-id",
),
(
"custom+scheme://host/api/v2/alias/upload/x/files/tus/y",
"y",
),
];
let bad = "https://example.com/api/v2/alias/upload//files/tus/FID456".to_owned(); // missing SID
assert!(FileID::try_from(bad).is_err());
for (good, expected_fid) in cases {
let s = good.to_string();
let file_id = FileID::try_from(s.clone()).expect("URL should parse successfully");
assert_eq!(
file_id.0, expected_fid,
"Expected `{}` → FileID({}), got {:?}",
good, expected_fid, file_id
);
}
}
let bad = "https://example.com/api/v2/alias/upload/SID123/files/tus/".to_owned(); // missing FID
assert!(FileID::try_from(bad).is_err());
#[test]
fn invalid_urls_return_error() {
let bad_inputs = vec![
// missing /api/v2/alias/upload
"http://example.com/files/tus/FID",
// missing /files/tus
"http://example.com/api/v2/alias/upload/SID123/FID456",
// trailing slash (doesn't match `$`)
"http://example.com/api/v2/alias/upload/SID/files/tus/FID/",
// empty fid
"http://example.com/api/v2/alias/upload/SID/files/tus/",
// random string
"just-a-random-string",
];
for bad in bad_inputs {
let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse");
// make sure it's the Mismatch variant, and that it contains the original input
match err {
error::Error::Mismatch { expected, actual } => {
assert_eq!(
expected, "<proto>://<base>/api/v2/alias/upload/<share>/files/tus/<file>",
"Error should output expected format"
);
assert_eq!(actual, bad.to_string(), "Error should echo back the input");
}
_ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"),
}
}
}
}