Simplify cloud task base URL normalization (#49147)

Replace the trailing-slash removal loop in `normalize_base_url` with
`trim_end_matches('/')`, preserving existing behavior.

Add regression coverage for unchanged URLs, single and repeated trailing
slashes, empty and slash-only inputs, and ChatGPT URLs with and without
`/backend-api`.

GitOrigin-RevId: db71e80c4dcaac33b308a45684b14e7edbd71528
This commit is contained in:
akira-oai
2026-09-29 03:32:19 +00:00
committed by copyberry
parent 8d48f71922
commit 3a16c0b707
+34 -4
View File
@@ -31,10 +31,7 @@ pub fn append_error_log(message: impl AsRef<str>) {
/// - 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<Utc>, ts: DateTime<Utc>) -> Stri
pub fn format_relative_time_now(ts: DateTime<Utc>) -> 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:?}");
}
}
}