From e0c5b5517f1fd59cde143e9788974dd88600dcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:06:43 +0000 Subject: [PATCH] better unit testing for `FileID` --- src/sharry/api/id.rs | 64 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/src/sharry/api/id.rs b/src/sharry/api/id.rs index 4f88db3..074b4fe 100644 --- a/src/sharry/api/id.rs +++ b/src/sharry/api/id.rs @@ -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 valid‐looking 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, ":///api/v2/alias/upload//files/tus/", + "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:?}"), + } + } } }