aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2020-11-07 13:53:32 +0100
committerQuentin <quentin@deuxfleurs.fr>2020-11-07 13:53:32 +0100
commit8f4ada196559f6db17628842bd90e0023b7a4e77 (patch)
tree0b53add0d8fc1368aa1b04ecdba5d007619567fe
parentc9c699d377aae1d20107efd590c22290ad2b740b (diff)
downloadgarage-8f4ada196559f6db17628842bd90e0023b7a4e77.tar.gz
garage-8f4ada196559f6db17628842bd90e0023b7a4e77.zip
Add a test for parse_bucket_key
-rw-r--r--src/api/api_server.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/api/api_server.rs b/src/api/api_server.rs
index 6221d784..dfdd4687 100644
--- a/src/api/api_server.rs
+++ b/src/api/api_server.rs
@@ -250,6 +250,10 @@ async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Respon
}
}
+/// Extract the bucket name and the key name from an HTTP path
+///
+/// S3 internally manages only buckets and keys. This function splits
+/// an HTTP path to get the corresponding bucket name and key.
fn parse_bucket_key(path: &str) -> Result<(&str, Option<&str>), Error> {
let path = path.trim_start_matches('/');
@@ -265,3 +269,16 @@ fn parse_bucket_key(path: &str) -> Result<(&str, Option<&str>), Error> {
None => Ok((path, None)),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_bucket_with_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(())
+ }
+}