From e83cdef39c318e92c0093efec41a03e695e8e844 Mon Sep 17 00:00:00 2001 From: Anthony Oteri Date: Tue, 26 Sep 2023 22:55:46 -0400 Subject: [PATCH] Additional test coverage for parsing the registry arg --- src/main.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3dcd7e7..7adf62c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,9 +47,12 @@ const LATEST: &str = "latest"; fn parse_registry_arg(host: &str) -> Result { log::trace!("make_registry_url(host: {host})"); - Url::parse(host) - .or_else(|_| Url::parse(&format!("https://{host}"))) - .or(Err(DredgeError::RegistryUrlError(host.to_string()))) + let mut host = String::from(host); + if !host.starts_with("http://") && !host.starts_with("https://") { + host = format!("https://{host}"); + } + + Url::parse(&host).or(Err(DredgeError::RegistryUrlError(host.to_string()))) } #[async_std::main] @@ -87,3 +90,70 @@ async fn main() -> Result<(), DredgeError> { Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + /// Test that given a valid URL in the argument, we return the + /// same URL from `parse_registry_arg()` + #[test] + fn test_parse_valid_url_registry_arg() { + let host = "https://example.com/registry"; + let result = parse_registry_arg(host); + + // Check if the result is Ok and contains the expected URL + assert!(result.is_ok()); + let url = result.unwrap(); + assert_eq!(url.scheme(), "https"); + assert_eq!(url.host_str(), Some("example.com")); + assert_eq!(url.path(), "/registry"); + } + + /// Test that given only an FQDN for a specific host in the + /// argument, we return an HTTPS url with that FQDN as the host_str. + #[test] + fn test_parse_valid_fqdn_registry_arg() { + let host = "example.com"; + let result = parse_registry_arg(host); + + // Check if the result is Ok and contains the expected URL + assert!(result.is_ok()); + let url = result.unwrap(); + assert_eq!(url.scheme(), "https"); + assert_eq!(url.host_str(), Some("example.com")); + assert_eq!(url.path(), "/"); + } + + /// Test that given an FQDN with port for a specific host in the + /// argument, we return an HTTPS url with that FQDN as the host and the + /// given port as the parsed port number. + #[test] + fn test_parse_valid_fqdn_registry_arg_alt_port() { + let host = "example.com:5123"; + let result = parse_registry_arg(host); + + // Check if the result is Ok and contains the expected URL + assert!(result.is_ok()); + let url = result.unwrap(); + assert_eq!(url.scheme(), "https"); + assert_eq!(url.host_str(), Some("example.com")); + assert_eq!(url.port(), Some(5123)); + assert_eq!(url.path(), "/"); + } + + /// Test that given an arbitrary string which can not be parsed as a valid + /// URL or FQDN, we return the `RegistryUrlError` variant. + #[test] + fn test_parse_invalid_registry_arg() { + let host = "///"; // This is not a valid URL + let result = parse_registry_arg(host); + + // Check if result is Err and matches the expected error variant. + assert!(result.is_err()); + match result { + Err(DredgeError::RegistryUrlError(_)) => {} // Expected error variant, + _ => panic!("Expected RegistryUrlError, got a different error"), + } + } +}