From 039efc570384c87fcb3442cf3b0a2d5f930d92ef Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 10 Mar 2019 11:04:50 -0700 Subject: [PATCH] move tests to different mods --- src/extract/mod.rs | 52 ------------------------------------------ src/extract/path.rs | 42 ++++++++++++++++++++++++++++++++++ src/extract/payload.rs | 24 ++++++++++++++++++- 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/src/extract/mod.rs b/src/extract/mod.rs index d8958b2d..78c6ba79 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs @@ -256,26 +256,6 @@ mod tests { hello: String, } - #[test] - fn test_bytes() { - let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") - .set_payload(Bytes::from_static(b"hello=world")) - .to_from(); - - let s = block_on(Bytes::from_request(&mut req)).unwrap(); - assert_eq!(s, Bytes::from_static(b"hello=world")); - } - - #[test] - fn test_string() { - let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") - .set_payload(Bytes::from_static(b"hello=world")) - .to_from(); - - let s = block_on(String::from_request(&mut req)).unwrap(); - assert_eq!(s, "hello=world"); - } - #[test] fn test_option() { let mut req = TestRequest::with_header( @@ -400,36 +380,4 @@ mod tests { assert_eq!(res[1], "32".to_owned()); } - #[test] - fn test_extract_path_single() { - let resource = ResourceDef::new("/{value}/"); - - let mut req = TestRequest::with_uri("/32/").to_from(); - resource.match_path(req.match_info_mut()); - - assert_eq!(*Path::::from_request(&mut req).unwrap(), 32); - } - - #[test] - fn test_tuple_extract() { - let resource = ResourceDef::new("/{key}/{value}/"); - - let mut req = TestRequest::with_uri("/name/user1/?id=test").to_from(); - resource.match_path(req.match_info_mut()); - - let res = block_on(<(Path<(String, String)>,)>::from_request(&mut req)).unwrap(); - assert_eq!((res.0).0, "name"); - assert_eq!((res.0).1, "user1"); - - let res = block_on( - <(Path<(String, String)>, Path<(String, String)>)>::from_request(&mut req), - ) - .unwrap(); - assert_eq!((res.0).0, "name"); - assert_eq!((res.0).1, "user1"); - assert_eq!((res.1).0, "name"); - assert_eq!((res.1).1, "user1"); - - let () = <()>::from_request(&mut req).unwrap(); - } } diff --git a/src/extract/path.rs b/src/extract/path.rs index d9461263..fc6811c7 100644 --- a/src/extract/path.rs +++ b/src/extract/path.rs @@ -174,3 +174,45 @@ where Self::extract(req).map_err(ErrorNotFound) } } + +#[cfg(test)] +mod tests { + use actix_router::ResourceDef; + + use super::*; + use crate::test::{block_on, TestRequest}; + + #[test] + fn test_extract_path_single() { + let resource = ResourceDef::new("/{value}/"); + + let mut req = TestRequest::with_uri("/32/").to_from(); + resource.match_path(req.match_info_mut()); + + assert_eq!(*Path::::from_request(&mut req).unwrap(), 32); + } + + #[test] + fn test_tuple_extract() { + let resource = ResourceDef::new("/{key}/{value}/"); + + let mut req = TestRequest::with_uri("/name/user1/?id=test").to_from(); + resource.match_path(req.match_info_mut()); + + let res = block_on(<(Path<(String, String)>,)>::from_request(&mut req)).unwrap(); + assert_eq!((res.0).0, "name"); + assert_eq!((res.0).1, "user1"); + + let res = block_on( + <(Path<(String, String)>, Path<(String, String)>)>::from_request(&mut req), + ) + .unwrap(); + assert_eq!((res.0).0, "name"); + assert_eq!((res.0).1, "user1"); + assert_eq!((res.1).0, "name"); + assert_eq!((res.1).1, "user1"); + + let () = <()>::from_request(&mut req).unwrap(); + } + +} diff --git a/src/extract/payload.rs b/src/extract/payload.rs index 82024c0d..13532eee 100644 --- a/src/extract/payload.rs +++ b/src/extract/payload.rs @@ -289,9 +289,11 @@ impl Default for PayloadConfig { #[cfg(test)] mod tests { + use bytes::Bytes; + use super::*; use crate::http::header; - use crate::test::TestRequest; + use crate::test::{block_on, TestRequest}; #[test] fn test_payload_config() { @@ -310,4 +312,24 @@ mod tests { TestRequest::with_header(header::CONTENT_TYPE, "application/json").to_from(); assert!(cfg.check_mimetype(&req).is_ok()); } + + #[test] + fn test_bytes() { + let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") + .set_payload(Bytes::from_static(b"hello=world")) + .to_from(); + + let s = block_on(Bytes::from_request(&mut req)).unwrap(); + assert_eq!(s, Bytes::from_static(b"hello=world")); + } + + #[test] + fn test_string() { + let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") + .set_payload(Bytes::from_static(b"hello=world")) + .to_from(); + + let s = block_on(String::from_request(&mut req)).unwrap(); + assert_eq!(s, "hello=world"); + } }