From e659e09e29228bad11041089d94e4bad35a57f1d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 18 Apr 2019 11:01:04 -0700 Subject: [PATCH] update tests --- src/extract.rs | 57 +-------------------------------------------- src/types/form.rs | 4 ---- src/types/path.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++ src/types/query.rs | 32 +++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 60 deletions(-) diff --git a/src/extract.rs b/src/extract.rs index 9023ea49..6d414fbc 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -265,13 +265,12 @@ tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, #[cfg(test)] mod tests { use actix_http::http::header; - use actix_router::ResourceDef; use bytes::Bytes; use serde_derive::Deserialize; use super::*; use crate::test::{block_on, TestRequest}; - use crate::types::{Form, FormConfig, Path, Query}; + use crate::types::{Form, FormConfig}; #[derive(Deserialize, Debug, PartialEq)] struct Info { @@ -350,58 +349,4 @@ mod tests { block_on(Result::, Error>::from_request(&req, &mut pl)).unwrap(); assert!(r.is_err()); } - - #[derive(Deserialize)] - struct MyStruct { - key: String, - value: String, - } - - #[derive(Deserialize)] - struct Id { - id: String, - } - - #[derive(Deserialize)] - struct Test2 { - key: String, - value: u32, - } - - #[test] - fn test_request_extract() { - let mut req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request(); - - let resource = ResourceDef::new("/{key}/{value}/"); - resource.match_path(req.match_info_mut()); - - let (req, mut pl) = req.into_parts(); - let s = Path::::from_request(&req, &mut pl).unwrap(); - assert_eq!(s.key, "name"); - assert_eq!(s.value, "user1"); - - let s = Path::<(String, String)>::from_request(&req, &mut pl).unwrap(); - assert_eq!(s.0, "name"); - assert_eq!(s.1, "user1"); - - let s = Query::::from_request(&req, &mut pl).unwrap(); - assert_eq!(s.id, "test"); - - let mut req = TestRequest::with_uri("/name/32/").to_srv_request(); - let resource = ResourceDef::new("/{key}/{value}/"); - resource.match_path(req.match_info_mut()); - - let (req, mut pl) = req.into_parts(); - let s = Path::::from_request(&req, &mut pl).unwrap(); - assert_eq!(s.as_ref().key, "name"); - assert_eq!(s.value, 32); - - let s = Path::<(String, u8)>::from_request(&req, &mut pl).unwrap(); - assert_eq!(s.0, "name"); - assert_eq!(s.1, 32); - - let res = Path::>::from_request(&req, &mut pl).unwrap(); - assert_eq!(res[0], "name".to_owned()); - assert_eq!(res[1], "32".to_owned()); - } } diff --git a/src/types/form.rs b/src/types/form.rs index 249f33b3..e8f78c49 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -331,10 +331,6 @@ mod tests { fn eq(err: UrlencodedError, other: UrlencodedError) -> bool { match err { - UrlencodedError::Chunked => match other { - UrlencodedError::Chunked => true, - _ => false, - }, UrlencodedError::Overflow => match other { UrlencodedError::Overflow => true, _ => false, diff --git a/src/types/path.rs b/src/types/path.rs index 13a35d5e..5f0a05af 100644 --- a/src/types/path.rs +++ b/src/types/path.rs @@ -171,10 +171,25 @@ where #[cfg(test)] mod tests { use actix_router::ResourceDef; + use derive_more::Display; + use serde_derive::Deserialize; use super::*; use crate::test::{block_on, TestRequest}; + #[derive(Deserialize, Debug, Display)] + #[display(fmt = "MyStruct({}, {})", key, value)] + struct MyStruct { + key: String, + value: String, + } + + #[derive(Deserialize)] + struct Test2 { + key: String, + value: u32, + } + #[test] fn test_extract_path_single() { let resource = ResourceDef::new("/{value}/"); @@ -184,6 +199,7 @@ mod tests { let (req, mut pl) = req.into_parts(); assert_eq!(*Path::::from_request(&req, &mut pl).unwrap(), 32); + assert!(Path::::from_request(&req, &mut pl).is_err()); } #[test] @@ -213,4 +229,46 @@ mod tests { let () = <()>::from_request(&req, &mut pl).unwrap(); } + #[test] + fn test_request_extract() { + let mut req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request(); + + let resource = ResourceDef::new("/{key}/{value}/"); + resource.match_path(req.match_info_mut()); + + let (req, mut pl) = req.into_parts(); + let mut s = Path::::from_request(&req, &mut pl).unwrap(); + assert_eq!(s.key, "name"); + assert_eq!(s.value, "user1"); + s.value = "user2".to_string(); + assert_eq!(s.value, "user2"); + assert_eq!( + format!("{}, {:?}", s, s), + "MyStruct(name, user2), MyStruct { key: \"name\", value: \"user2\" }" + ); + let s = s.into_inner(); + assert_eq!(s.value, "user2"); + + let s = Path::<(String, String)>::from_request(&req, &mut pl).unwrap(); + assert_eq!(s.0, "name"); + assert_eq!(s.1, "user1"); + + let mut req = TestRequest::with_uri("/name/32/").to_srv_request(); + let resource = ResourceDef::new("/{key}/{value}/"); + resource.match_path(req.match_info_mut()); + + let (req, mut pl) = req.into_parts(); + let s = Path::::from_request(&req, &mut pl).unwrap(); + assert_eq!(s.as_ref().key, "name"); + assert_eq!(s.value, 32); + + let s = Path::<(String, u8)>::from_request(&req, &mut pl).unwrap(); + assert_eq!(s.0, "name"); + assert_eq!(s.1, 32); + + let res = Path::>::from_request(&req, &mut pl).unwrap(); + assert_eq!(res[0], "name".to_owned()); + assert_eq!(res[1], "32".to_owned()); + } + } diff --git a/src/types/query.rs b/src/types/query.rs index 596254be..f9f545d6 100644 --- a/src/types/query.rs +++ b/src/types/query.rs @@ -133,3 +133,35 @@ where }) } } + +#[cfg(test)] +mod tests { + use derive_more::Display; + use serde_derive::Deserialize; + + use super::*; + use crate::test::TestRequest; + + #[derive(Deserialize, Debug, Display)] + struct Id { + id: String, + } + + #[test] + fn test_request_extract() { + let req = TestRequest::with_uri("/name/user1/").to_srv_request(); + let (req, mut pl) = req.into_parts(); + assert!(Query::::from_request(&req, &mut pl).is_err()); + + let req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request(); + let (req, mut pl) = req.into_parts(); + + let mut s = Query::::from_request(&req, &mut pl).unwrap(); + assert_eq!(s.id, "test"); + assert_eq!(format!("{}, {:?}", s, s), "test, Id { id: \"test\" }"); + + s.id = "test1".to_string(); + let s = s.into_inner(); + assert_eq!(s.id, "test1"); + } +}