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

update tests

This commit is contained in:
Nikolay Kim 2019-04-18 11:01:04 -07:00
parent 163ca89cf4
commit e659e09e29
4 changed files with 91 additions and 60 deletions

View File

@ -265,13 +265,12 @@ tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5,
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use actix_http::http::header; use actix_http::http::header;
use actix_router::ResourceDef;
use bytes::Bytes; use bytes::Bytes;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use super::*; use super::*;
use crate::test::{block_on, TestRequest}; use crate::test::{block_on, TestRequest};
use crate::types::{Form, FormConfig, Path, Query}; use crate::types::{Form, FormConfig};
#[derive(Deserialize, Debug, PartialEq)] #[derive(Deserialize, Debug, PartialEq)]
struct Info { struct Info {
@ -350,58 +349,4 @@ mod tests {
block_on(Result::<Form<Info>, Error>::from_request(&req, &mut pl)).unwrap(); block_on(Result::<Form<Info>, Error>::from_request(&req, &mut pl)).unwrap();
assert!(r.is_err()); 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::<MyStruct>::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::<Id>::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::<Test2>::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::<Vec<String>>::from_request(&req, &mut pl).unwrap();
assert_eq!(res[0], "name".to_owned());
assert_eq!(res[1], "32".to_owned());
}
} }

View File

@ -331,10 +331,6 @@ mod tests {
fn eq(err: UrlencodedError, other: UrlencodedError) -> bool { fn eq(err: UrlencodedError, other: UrlencodedError) -> bool {
match err { match err {
UrlencodedError::Chunked => match other {
UrlencodedError::Chunked => true,
_ => false,
},
UrlencodedError::Overflow => match other { UrlencodedError::Overflow => match other {
UrlencodedError::Overflow => true, UrlencodedError::Overflow => true,
_ => false, _ => false,

View File

@ -171,10 +171,25 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use actix_router::ResourceDef; use actix_router::ResourceDef;
use derive_more::Display;
use serde_derive::Deserialize;
use super::*; use super::*;
use crate::test::{block_on, TestRequest}; 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] #[test]
fn test_extract_path_single() { fn test_extract_path_single() {
let resource = ResourceDef::new("/{value}/"); let resource = ResourceDef::new("/{value}/");
@ -184,6 +199,7 @@ mod tests {
let (req, mut pl) = req.into_parts(); let (req, mut pl) = req.into_parts();
assert_eq!(*Path::<i8>::from_request(&req, &mut pl).unwrap(), 32); assert_eq!(*Path::<i8>::from_request(&req, &mut pl).unwrap(), 32);
assert!(Path::<MyStruct>::from_request(&req, &mut pl).is_err());
} }
#[test] #[test]
@ -213,4 +229,46 @@ mod tests {
let () = <()>::from_request(&req, &mut pl).unwrap(); 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::<MyStruct>::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::<Test2>::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::<Vec<String>>::from_request(&req, &mut pl).unwrap();
assert_eq!(res[0], "name".to_owned());
assert_eq!(res[1], "32".to_owned());
}
} }

View File

@ -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::<Id>::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::<Id>::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");
}
}