From f7807e43d8e89dd4243ca2323be75caaeb660244 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 10 Jan 2018 15:28:33 -0800 Subject: [PATCH] cleanup Binary type; more cors tests --- src/body.rs | 72 ++++++++++++------------------------------ src/middleware/cors.rs | 2 ++ 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/src/body.rs b/src/body.rs index 53af6e40..7eaa5446 100644 --- a/src/body.rs +++ b/src/body.rs @@ -31,13 +31,8 @@ pub enum Binary { Bytes(Bytes), /// Static slice Slice(&'static [u8]), - /// Shared bytes body - SharedBytes(Rc), /// Shared stirng body SharedString(Rc), - /// Shared bytes body - #[doc(hidden)] - ArcSharedBytes(Arc), /// Shared string body #[doc(hidden)] ArcSharedString(Arc), @@ -118,8 +113,6 @@ impl Binary { match *self { Binary::Bytes(ref bytes) => bytes.len(), Binary::Slice(slice) => slice.len(), - Binary::SharedBytes(ref bytes) => bytes.len(), - Binary::ArcSharedBytes(ref bytes) => bytes.len(), Binary::SharedString(ref s) => s.len(), Binary::ArcSharedString(ref s) => s.len(), } @@ -131,6 +124,17 @@ impl Binary { } } +impl Into for Binary { + fn into(self) -> Bytes { + match self { + Binary::Bytes(bytes) => bytes, + Binary::Slice(slice) => Bytes::from(slice), + Binary::SharedString(s) => Bytes::from(s.as_str()), + Binary::ArcSharedString(s) => Bytes::from(s.as_str()), + } + } +} + impl From<&'static str> for Binary { fn from(s: &'static str) -> Binary { Binary::Slice(s.as_ref()) @@ -173,30 +177,6 @@ impl From for Binary { } } -impl From> for Binary { - fn from(body: Rc) -> Binary { - Binary::SharedBytes(body) - } -} - -impl<'a> From<&'a Rc> for Binary { - fn from(body: &'a Rc) -> Binary { - Binary::SharedBytes(Rc::clone(body)) - } -} - -impl From> for Binary { - fn from(body: Arc) -> Binary { - Binary::ArcSharedBytes(body) - } -} - -impl<'a> From<&'a Arc> for Binary { - fn from(body: &'a Arc) -> Binary { - Binary::ArcSharedBytes(Arc::clone(body)) - } -} - impl From> for Binary { fn from(body: Rc) -> Binary { Binary::SharedString(body) @@ -226,8 +206,6 @@ impl AsRef<[u8]> for Binary { match *self { Binary::Bytes(ref bytes) => bytes.as_ref(), Binary::Slice(slice) => slice, - Binary::SharedBytes(ref bytes) => bytes.as_ref(), - Binary::ArcSharedBytes(ref bytes) => bytes.as_ref(), Binary::SharedString(ref s) => s.as_bytes(), Binary::ArcSharedString(ref s) => s.as_bytes(), } @@ -242,7 +220,6 @@ mod tests { fn test_body_is_streaming() { assert_eq!(Body::Empty.is_streaming(), false); assert_eq!(Body::Binary(Binary::from("")).is_streaming(), false); - // assert_eq!(Body::Streaming.is_streaming(), true); } #[test] @@ -277,15 +254,6 @@ mod tests { assert_eq!(Binary::from(Bytes::from("test")).as_ref(), "test".as_bytes()); } - #[test] - fn test_rc_bytes() { - let b = Rc::new(Bytes::from("test")); - assert_eq!(Binary::from(b.clone()).len(), 4); - assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(Binary::from(&b).len(), 4); - assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); - } - #[test] fn test_ref_string() { let b = Rc::new("test".to_owned()); @@ -302,15 +270,6 @@ mod tests { assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } - #[test] - fn test_arc_bytes() { - let b = Arc::new(Bytes::from("test")); - assert_eq!(Binary::from(b.clone()).len(), 4); - assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(Binary::from(&b).len(), 4); - assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); - } - #[test] fn test_arc_string() { let b = Arc::new("test".to_owned()); @@ -335,4 +294,13 @@ mod tests { assert_eq!(Binary::from(b.clone()).len(), 4); assert_eq!(Binary::from(b).as_ref(), "test".as_bytes()); } + + #[test] + fn test_binary_into() { + let bytes = Bytes::from_static(b"test"); + let b: Bytes = Binary::from("test").into(); + assert_eq!(b, bytes); + let b: Bytes = Binary::from(bytes.clone()).into(); + assert_eq!(b, bytes); + } } diff --git a/src/middleware/cors.rs b/src/middleware/cors.rs index b7970845..9b703cbf 100644 --- a/src/middleware/cors.rs +++ b/src/middleware/cors.rs @@ -794,6 +794,7 @@ mod tests { fn test_response() { let cors = Cors::build() .send_wildcard() + .disable_preflight() .max_age(3600) .allowed_methods(vec![Method::GET, Method::OPTIONS, Method::POST]) .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) @@ -823,6 +824,7 @@ mod tests { resp.headers().get(header::VARY).unwrap().as_bytes()); let cors = Cors::build() + .disable_vary_header() .allowed_origin("https://www.example.com") .finish().unwrap(); let resp: HttpResponse = HTTPOk.into();