From 7ee11f0eb6327ea809b7ca27cfdf0b1d70cdbde9 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 5 Jan 2022 17:34:48 +0100 Subject: [PATCH] Fix unit tests --- src/api/s3_bucket.rs | 22 ++++++++++++++++------ src/api/s3_router.rs | 34 ++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/api/s3_bucket.rs b/src/api/s3_bucket.rs index fb3e982d..3766bf86 100644 --- a/src/api/s3_bucket.rs +++ b/src/api/s3_bucket.rs @@ -287,20 +287,20 @@ fn parse_create_bucket_xml(xml_bytes: &[u8]) -> Option> { let xml = roxmltree::Document::parse(xml_str).ok()?; - let root = xml.root(); - let cbc = root.first_child()?; + let cbc = xml.root().first_child()?; if !cbc.has_tag_name("CreateBucketConfiguration") { return None; } let mut ret = None; for item in cbc.children() { + println!("{:?}", item); if item.has_tag_name("LocationConstraint") { if ret != None { return None; } ret = Some(item.text()?.to_string()); - } else { + } else if !item.is_text() { return None; } } @@ -313,7 +313,17 @@ mod tests { use super::*; #[test] - fn create_bucket() -> Result<(), ()> { + fn create_bucket() { + assert_eq!(parse_create_bucket_xml(br#""#), Some(None)); + assert_eq!( + parse_create_bucket_xml( + br#" + + + "# + ), + Some(None) + ); assert_eq!( parse_create_bucket_xml( br#" @@ -322,13 +332,13 @@ mod tests { "# ), - Some("Europe") + Some(Some("Europe".into())) ); assert_eq!( parse_create_bucket_xml( br#" - + "# ), None diff --git a/src/api/s3_router.rs b/src/api/s3_router.rs index f32aed30..e0264333 100644 --- a/src/api/s3_router.rs +++ b/src/api/s3_router.rs @@ -1000,13 +1000,13 @@ mod tests { $( assert!( matches!( - parse(stringify!($method), $uri, Some("my_bucket".to_owned()), None), + parse(test_cases!{@actual_method $method}, $uri, Some("my_bucket".to_owned()), None), Endpoint::$variant { .. } ) ); assert!( matches!( - parse(stringify!($method), concat!("/my_bucket", $uri), None, None), + parse(test_cases!{@actual_method $method}, concat!("/my_bucket", $uri), None, None), Endpoint::$variant { .. } ) ); @@ -1014,6 +1014,16 @@ mod tests { test_cases!{@auth $method $uri} )* }}; + + (@actual_method HEAD) => {{ "HEAD" }}; + (@actual_method GET) => {{ "GET" }}; + (@actual_method OWNER_GET) => {{ "GET" }}; + (@actual_method PUT) => {{ "PUT" }}; + (@actual_method OWNER_PUT) => {{ "PUT" }}; + (@actual_method POST) => {{ "POST" }}; + (@actual_method DELETE) => {{ "DELETE" }}; + (@actual_method OWNER_DELETE) => {{ "DELETE" }}; + (@auth HEAD $uri:expr) => {{ assert_eq!(parse("HEAD", concat!("/my_bucket", $uri), None, None).authorization_type(), Authorization::Read("my_bucket")) @@ -1022,10 +1032,18 @@ mod tests { assert_eq!(parse("GET", concat!("/my_bucket", $uri), None, None).authorization_type(), Authorization::Read("my_bucket")) }}; + (@auth OWNER_GET $uri:expr) => {{ + assert_eq!(parse("GET", concat!("/my_bucket", $uri), None, None).authorization_type(), + Authorization::Owner("my_bucket")) + }}; (@auth PUT $uri:expr) => {{ assert_eq!(parse("PUT", concat!("/my_bucket", $uri), None, None).authorization_type(), Authorization::Write("my_bucket")) }}; + (@auth OWNER_PUT $uri:expr) => {{ + assert_eq!(parse("PUT", concat!("/my_bucket", $uri), None, None).authorization_type(), + Authorization::Owner("my_bucket")) + }}; (@auth POST $uri:expr) => {{ assert_eq!(parse("POST", concat!("/my_bucket", $uri), None, None).authorization_type(), Authorization::Write("my_bucket")) @@ -1034,6 +1052,10 @@ mod tests { assert_eq!(parse("DELETE", concat!("/my_bucket", $uri), None, None).authorization_type(), Authorization::Write("my_bucket")) }}; + (@auth OWNER_DELETE $uri:expr) => {{ + assert_eq!(parse("DELETE", concat!("/my_bucket", $uri), None, None).authorization_type(), + Authorization::Owner("my_bucket")) + }}; } #[test] @@ -1109,7 +1131,7 @@ mod tests { PUT "/" => CreateBucket POST "/example-object?uploads" => CreateMultipartUpload POST "/{Key+}?uploads" => CreateMultipartUpload - DELETE "/" => DeleteBucket + OWNER_DELETE "/" => DeleteBucket DELETE "/?analytics&id=list1" => DeleteBucketAnalyticsConfiguration DELETE "/?analytics&id=Id" => DeleteBucketAnalyticsConfiguration DELETE "/?cors" => DeleteBucketCors @@ -1124,7 +1146,7 @@ mod tests { DELETE "/?policy" => DeleteBucketPolicy DELETE "/?replication" => DeleteBucketReplication DELETE "/?tagging" => DeleteBucketTagging - DELETE "/?website" => DeleteBucketWebsite + OWNER_DELETE "/?website" => DeleteBucketWebsite DELETE "/my-second-image.jpg" => DeleteObject DELETE "/my-third-image.jpg?versionId=UIORUnfndfiufdisojhr398493jfdkjFJjkndnqUifhnw89493jJFJ" => DeleteObject DELETE "/Key+?versionId=VersionId" => DeleteObject @@ -1153,7 +1175,7 @@ mod tests { GET "/?requestPayment" => GetBucketRequestPayment GET "/?tagging" => GetBucketTagging GET "/?versioning" => GetBucketVersioning - GET "/?website" => GetBucketWebsite + OWNER_GET "/?website" => GetBucketWebsite GET "/my-image.jpg" => GetObject GET "/myObject?versionId=3/L4kqtJlcpXroDTDmpUMLUo" => GetObject GET "/Junk3.txt?response-cache-control=No-cache&response-content-disposition=attachment%3B%20filename%3Dtesting.txt&response-content-encoding=x-gzip&response-content-language=mi%2C%20en&response-expires=Thu%2C%2001%20Dec%201994%2016:00:00%20GMT" => GetObject @@ -1227,7 +1249,7 @@ mod tests { PUT "/?requestPayment" => PutBucketRequestPayment PUT "/?tagging" => PutBucketTagging PUT "/?versioning" => PutBucketVersioning - PUT "/?website" => PutBucketWebsite + OWNER_PUT "/?website" => PutBucketWebsite PUT "/my-image.jpg" => PutObject PUT "/Key+" => PutObject PUT "/my-image.jpg?acl" => PutObjectAcl