1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

cleanup Binary type; more cors tests

This commit is contained in:
Nikolay Kim 2018-01-10 15:28:33 -08:00
parent fee54d1de0
commit f7807e43d8
2 changed files with 22 additions and 52 deletions

View File

@ -31,13 +31,8 @@ pub enum Binary {
Bytes(Bytes),
/// Static slice
Slice(&'static [u8]),
/// Shared bytes body
SharedBytes(Rc<Bytes>),
/// Shared stirng body
SharedString(Rc<String>),
/// Shared bytes body
#[doc(hidden)]
ArcSharedBytes(Arc<Bytes>),
/// Shared string body
#[doc(hidden)]
ArcSharedString(Arc<String>),
@ -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<Bytes> 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<BytesMut> for Binary {
}
}
impl From<Rc<Bytes>> for Binary {
fn from(body: Rc<Bytes>) -> Binary {
Binary::SharedBytes(body)
}
}
impl<'a> From<&'a Rc<Bytes>> for Binary {
fn from(body: &'a Rc<Bytes>) -> Binary {
Binary::SharedBytes(Rc::clone(body))
}
}
impl From<Arc<Bytes>> for Binary {
fn from(body: Arc<Bytes>) -> Binary {
Binary::ArcSharedBytes(body)
}
}
impl<'a> From<&'a Arc<Bytes>> for Binary {
fn from(body: &'a Arc<Bytes>) -> Binary {
Binary::ArcSharedBytes(Arc::clone(body))
}
}
impl From<Rc<String>> for Binary {
fn from(body: Rc<String>) -> 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);
}
}

View File

@ -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();