diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 6221d784c..9dc74dac8 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -72,11 +72,6 @@ async fn handler_inner(garage: Arc, req: Request) -> Result, req: Request) -> Result Result<(&str, Option<&str>), Error> { let path = path.trim_start_matches('/'); - match path.find('/') { + let (bucket, key) = match path.find('/') { Some(i) => { let key = &path[i + 1..]; if key.len() > 0 { - Ok((&path[..i], Some(key))) + (&path[..i], Some(key)) } else { - Ok((&path[..i], None)) + (&path[..i], None) } } - None => Ok((path, None)), + None => (path, None), + }; + if bucket.len() == 0 { + return Err(Error::BadRequest(format!("No bucket specified"))); + } + Ok((bucket, key)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_bucket_containing_a_key() -> Result<(), Error> { + let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg")?; + assert_eq!(bucket, "my_bucket"); + assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); + Ok(()) + } + + #[test] + fn parse_bucket_containing_no_key() -> Result<(), Error> { + let (bucket, key) = parse_bucket_key("/my_bucket/")?; + assert_eq!(bucket, "my_bucket"); + assert!(key.is_none()); + let (bucket, key) = parse_bucket_key("/my_bucket")?; + assert_eq!(bucket, "my_bucket"); + assert!(key.is_none()); + Ok(()) + } + + #[test] + fn parse_bucket_containing_no_bucket() { + let parsed = parse_bucket_key(""); + assert!(parsed.is_err()); + let parsed = parse_bucket_key("/"); + assert!(parsed.is_err()); + let parsed = parse_bucket_key("////"); + assert!(parsed.is_err()); } }