diff --git a/codex-rs/cloud-tasks/src/util.rs b/codex-rs/cloud-tasks/src/util.rs index 812d0b4b9a..d041d8ce10 100644 --- a/codex-rs/cloud-tasks/src/util.rs +++ b/codex-rs/cloud-tasks/src/util.rs @@ -31,10 +31,7 @@ pub fn append_error_log(message: impl AsRef) { /// - trims trailing '/' /// - appends '/backend-api' for ChatGPT hosts when missing pub fn normalize_base_url(input: &str) -> String { - let mut base_url = input.to_string(); - while base_url.ends_with('/') { - base_url.pop(); - } + let mut base_url = input.trim_end_matches('/').to_string(); if (base_url.starts_with("https://chatgpt.com") || base_url.starts_with("https://chat.openai.com")) && !base_url.contains("/backend-api") @@ -171,3 +168,36 @@ pub fn format_relative_time(reference: DateTime, ts: DateTime) -> Stri pub fn format_relative_time_now(ts: DateTime) -> String { format_relative_time(Utc::now(), ts) } + +#[cfg(test)] +mod tests { + use pretty_assertions::assert_eq; + + use super::normalize_base_url; + + #[test] + fn normalize_base_url_normalizes_urls() { + for (input, expected) in [ + ("https://example.com/path", "https://example.com/path"), + ("https://example.com/path/", "https://example.com/path"), + ("https://example.com/path///", "https://example.com/path"), + ("", ""), + ("///", ""), + ("https://chatgpt.com///", "https://chatgpt.com/backend-api"), + ( + "https://chatgpt.com/backend-api///", + "https://chatgpt.com/backend-api", + ), + ( + "https://chat.openai.com///", + "https://chat.openai.com/backend-api", + ), + ( + "https://chat.openai.com/backend-api///", + "https://chat.openai.com/backend-api", + ), + ] { + assert_eq!(normalize_base_url(input), expected, "input: {input:?}"); + } + } +}