Skip to content

Commit d6b14d0

Browse files
committed
Storage: Extract apply_cdn_prefix() fn to make testing easier
1 parent 3c7cef3 commit d6b14d0

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/storage.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -218,26 +218,14 @@ impl Storage {
218218
///
219219
/// The function doesn't check for the existence of the file.
220220
pub fn crate_location(&self, name: &str, version: &str) -> String {
221-
self.with_cdn_prefix(&crate_file_path(name, version))
222-
.replace('+', "%2B")
221+
apply_cdn_prefix(&self.cdn_prefix, &crate_file_path(name, version)).replace('+', "%2B")
223222
}
224223

225224
/// Returns the URL of an uploaded crate's version readme.
226225
///
227226
/// The function doesn't check for the existence of the file.
228227
pub fn readme_location(&self, name: &str, version: &str) -> String {
229-
self.with_cdn_prefix(&readme_path(name, version))
230-
.replace('+', "%2B")
231-
}
232-
233-
fn with_cdn_prefix(&self, path: &Path) -> String {
234-
match self.cdn_prefix.as_ref() {
235-
Some(cdn_prefix) if !cdn_prefix.starts_with("https://") => {
236-
format!("https://{cdn_prefix}/{path}")
237-
}
238-
Some(cdn_prefix) => format!("{cdn_prefix}/{path}"),
239-
None => format!("/{path}"),
240-
}
228+
apply_cdn_prefix(&self.cdn_prefix, &readme_path(name, version)).replace('+', "%2B")
241229
}
242230

243231
#[instrument(skip(self))]
@@ -357,6 +345,16 @@ fn readme_path(name: &str, version: &str) -> Path {
357345
format!("{PREFIX_READMES}/{name}/{name}-{version}.html").into()
358346
}
359347

348+
fn apply_cdn_prefix(cdn_prefix: &Option<String>, path: &Path) -> String {
349+
match cdn_prefix {
350+
Some(cdn_prefix) if !cdn_prefix.starts_with("https://") => {
351+
format!("https://{cdn_prefix}/{path}")
352+
}
353+
Some(cdn_prefix) => format!("{cdn_prefix}/{path}"),
354+
None => format!("/{path}"),
355+
}
356+
}
357+
360358
#[cfg(test)]
361359
mod tests {
362360
use super::*;
@@ -422,6 +420,32 @@ mod tests {
422420
}
423421
}
424422

423+
#[test]
424+
fn cdn_prefix() {
425+
assert_eq!(apply_cdn_prefix(&None, &"foo".into()), "/foo");
426+
assert_eq!(
427+
apply_cdn_prefix(&Some("static.crates.io".to_string()), &"foo".into()),
428+
"https://static.crates.io/foo"
429+
);
430+
assert_eq!(
431+
apply_cdn_prefix(
432+
&Some("https://fastly-static.crates.io".to_string()),
433+
&"foo".into()
434+
),
435+
"https://fastly-static.crates.io/foo"
436+
);
437+
438+
assert_eq!(
439+
apply_cdn_prefix(&Some("static.crates.io".to_string()), &"/foo/bar".into()),
440+
"https://static.crates.io/foo/bar"
441+
);
442+
443+
assert_eq!(
444+
apply_cdn_prefix(&Some("static.crates.io/".to_string()), &"/foo/bar".into()),
445+
"https://static.crates.io//foo/bar"
446+
);
447+
}
448+
425449
#[tokio::test]
426450
async fn delete_all_crate_files() {
427451
let storage = prepare().await;

0 commit comments

Comments
 (0)