1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

re-enable extractor tests

This commit is contained in:
Nikolay Kim 2019-03-03 15:32:47 -08:00
parent 360082f99f
commit 8502c32a3c
3 changed files with 206 additions and 216 deletions

View File

@ -1044,11 +1044,11 @@ impl Default for PayloadConfig {
#[doc(hidden)] #[doc(hidden)]
impl<P> FromRequest<P> for () { impl<P> FromRequest<P> for () {
type Error = Error; type Error = Error;
type Future = FutureResult<(), Error>; type Future = Result<(), Error>;
type Config = (); type Config = ();
fn from_request(_req: &mut ServiceFromRequest<P>) -> Self::Future { fn from_request(_req: &mut ServiceFromRequest<P>) -> Self::Future {
ok(()) Ok(())
} }
} }
@ -1147,6 +1147,7 @@ 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;
@ -1194,218 +1195,196 @@ mod tests {
let s = rt.block_on(Form::<Info>::from_request(&mut req)).unwrap(); let s = rt.block_on(Form::<Info>::from_request(&mut req)).unwrap();
assert_eq!(s.hello, "world"); assert_eq!(s.hello, "world");
} }
#[test]
fn test_option() {
let mut rt = actix_rt::Runtime::new().unwrap();
let mut req = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.config(FormConfig::default().limit(4096))
.to_from();
let r = rt
.block_on(Option::<Form<Info>>::from_request(&mut req))
.unwrap();
assert_eq!(r, None);
let mut req = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "9")
.set_payload(Bytes::from_static(b"hello=world"))
.to_from();
let r = rt
.block_on(Option::<Form<Info>>::from_request(&mut req))
.unwrap();
assert_eq!(
r,
Some(Form(Info {
hello: "world".into()
}))
);
let mut req = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "9")
.set_payload(Bytes::from_static(b"bye=world"))
.to_from();
let r = rt
.block_on(Option::<Form<Info>>::from_request(&mut req))
.unwrap();
assert_eq!(r, None);
} }
// #[test] #[test]
// fn test_option() { fn test_result() {
// let req = TestRequest::with_header( let mut rt = actix_rt::Runtime::new().unwrap();
// header::CONTENT_TYPE, let mut req = TestRequest::with_header(
// "application/x-www-form-urlencoded", header::CONTENT_TYPE,
// ) "application/x-www-form-urlencoded",
// .finish(); )
.header(header::CONTENT_LENGTH, "11")
.set_payload(Bytes::from_static(b"hello=world"))
.to_from();
// let mut cfg = FormConfig::default(); let r = rt
// cfg.limit(4096); .block_on(Result::<Form<Info>, Error>::from_request(&mut req))
.unwrap()
.unwrap();
assert_eq!(
r,
Form(Info {
hello: "world".into()
})
);
// match Option::<Form<Info>>::from_request(&req, &cfg) let mut req = TestRequest::with_header(
// .poll() header::CONTENT_TYPE,
// .unwrap() "application/x-www-form-urlencoded",
// { )
// Async::Ready(r) => assert_eq!(r, None), .header(header::CONTENT_LENGTH, "9")
// _ => unreachable!(), .set_payload(Bytes::from_static(b"bye=world"))
// } .to_from();
// let req = TestRequest::with_header( let r = rt
// header::CONTENT_TYPE, .block_on(Result::<Form<Info>, Error>::from_request(&mut req))
// "application/x-www-form-urlencoded", .unwrap();
// ) assert!(r.is_err());
// .header(header::CONTENT_LENGTH, "9") }
// .set_payload(Bytes::from_static(b"hello=world"))
// .finish();
// match Option::<Form<Info>>::from_request(&req, &cfg) #[test]
// .poll() fn test_payload_config() {
// .unwrap() let req = TestRequest::default().to_from();
// { let cfg = PayloadConfig::default().mimetype(mime::APPLICATION_JSON);
// Async::Ready(r) => assert_eq!( assert!(cfg.check_mimetype(&req).is_err());
// r,
// Some(Form(Info {
// hello: "world".into()
// }))
// ),
// _ => unreachable!(),
// }
// let req = TestRequest::with_header( let req = TestRequest::with_header(
// header::CONTENT_TYPE, header::CONTENT_TYPE,
// "application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
// ) )
// .header(header::CONTENT_LENGTH, "9") .to_from();
// .set_payload(Bytes::from_static(b"bye=world")) assert!(cfg.check_mimetype(&req).is_err());
// .finish();
// match Option::<Form<Info>>::from_request(&req, &cfg) let req =
// .poll() TestRequest::with_header(header::CONTENT_TYPE, "application/json").to_from();
// .unwrap() assert!(cfg.check_mimetype(&req).is_ok());
// { }
// Async::Ready(r) => assert_eq!(r, None),
// _ => unreachable!(),
// }
// }
// #[test] #[derive(Deserialize)]
// fn test_result() { struct MyStruct {
// let req = TestRequest::with_header( key: String,
// header::CONTENT_TYPE, value: String,
// "application/x-www-form-urlencoded", }
// )
// .header(header::CONTENT_LENGTH, "11")
// .set_payload(Bytes::from_static(b"hello=world"))
// .finish();
// match Result::<Form<Info>, Error>::from_request(&req, &FormConfig::default()) #[derive(Deserialize)]
// .poll() struct Id {
// .unwrap() id: String,
// { }
// Async::Ready(Ok(r)) => assert_eq!(
// r,
// Form(Info {
// hello: "world".into()
// })
// ),
// _ => unreachable!(),
// }
// let req = TestRequest::with_header( #[derive(Deserialize)]
// header::CONTENT_TYPE, struct Test2 {
// "application/x-www-form-urlencoded", key: String,
// ) value: u32,
// .header(header::CONTENT_LENGTH, "9") }
// .set_payload(Bytes::from_static(b"bye=world"))
// .finish();
// match Result::<Form<Info>, Error>::from_request(&req, &FormConfig::default()) #[test]
// .poll() fn test_request_extract() {
// .unwrap() let mut req = TestRequest::with_uri("/name/user1/?id=test").to_from();
// {
// Async::Ready(r) => assert!(r.is_err()),
// _ => unreachable!(),
// }
// }
// #[test] let resource = ResourceDef::new("/{key}/{value}/");
// fn test_payload_config() { resource.match_path(req.match_info_mut());
// let req = TestRequest::default().finish();
// let mut cfg = PayloadConfig::default();
// cfg.mimetype(mime::APPLICATION_JSON);
// assert!(cfg.check_mimetype(&req).is_err());
// let req = TestRequest::with_header( let s = Path::<MyStruct>::from_request(&mut req).unwrap();
// header::CONTENT_TYPE, assert_eq!(s.key, "name");
// "application/x-www-form-urlencoded", assert_eq!(s.value, "user1");
// )
// .finish();
// assert!(cfg.check_mimetype(&req).is_err());
// let req = let s = Path::<(String, String)>::from_request(&mut req).unwrap();
// TestRequest::with_header(header::CONTENT_TYPE, "application/json").finish(); assert_eq!(s.0, "name");
// assert!(cfg.check_mimetype(&req).is_ok()); assert_eq!(s.1, "user1");
// }
// #[derive(Deserialize)] let s = Query::<Id>::from_request(&mut req).unwrap();
// struct MyStruct { assert_eq!(s.id, "test");
// key: String,
// value: String,
// }
// #[derive(Deserialize)] let mut req = TestRequest::with_uri("/name/32/").to_from();
// struct Id { let resource = ResourceDef::new("/{key}/{value}/");
// id: String, resource.match_path(req.match_info_mut());
// }
// #[derive(Deserialize)] let s = Path::<Test2>::from_request(&mut req).unwrap();
// struct Test2 { assert_eq!(s.as_ref().key, "name");
// key: String, assert_eq!(s.value, 32);
// value: u32,
// }
// #[test] let s = Path::<(String, u8)>::from_request(&mut req).unwrap();
// fn test_request_extract() { assert_eq!(s.0, "name");
// let req = TestRequest::with_uri("/name/user1/?id=test").finish(); assert_eq!(s.1, 32);
// let mut router = Router::<()>::default(); let res = Path::<Vec<String>>::from_request(&mut req).unwrap();
// router.register_resource(Resource::new(ResourceDef::new("/{key}/{value}/"))); assert_eq!(res[0], "name".to_owned());
// let info = router.recognize(&req, &(), 0); assert_eq!(res[1], "32".to_owned());
// let req = req.with_route_info(info); }
// let s = Path::<MyStruct>::from_request(&req, &()).unwrap(); #[test]
// assert_eq!(s.key, "name"); fn test_extract_path_single() {
// assert_eq!(s.value, "user1"); let resource = ResourceDef::new("/{value}/");
// let s = Path::<(String, String)>::from_request(&req, &()).unwrap(); let mut req = TestRequest::with_uri("/32/").to_from();
// assert_eq!(s.0, "name"); resource.match_path(req.match_info_mut());
// assert_eq!(s.1, "user1");
// let s = Query::<Id>::from_request(&req, &()).unwrap(); assert_eq!(*Path::<i8>::from_request(&mut req).unwrap(), 32);
// assert_eq!(s.id, "test"); }
// let mut router = Router::<()>::default(); #[test]
// router.register_resource(Resource::new(ResourceDef::new("/{key}/{value}/"))); fn test_tuple_extract() {
// let req = TestRequest::with_uri("/name/32/").finish(); let mut rt = actix_rt::Runtime::new().unwrap();
// let info = router.recognize(&req, &(), 0); let resource = ResourceDef::new("/{key}/{value}/");
// let req = req.with_route_info(info);
// let s = Path::<Test2>::from_request(&req, &()).unwrap(); let mut req = TestRequest::with_uri("/name/user1/?id=test").to_from();
// assert_eq!(s.as_ref().key, "name"); resource.match_path(req.match_info_mut());
// assert_eq!(s.value, 32);
// let s = Path::<(String, u8)>::from_request(&req, &()).unwrap(); let res = rt
// assert_eq!(s.0, "name"); .block_on(<(Path<(String, String)>,)>::from_request(&mut req))
// assert_eq!(s.1, 32); .unwrap();
assert_eq!((res.0).0, "name");
assert_eq!((res.0).1, "user1");
// let res = Path::<Vec<String>>::extract(&req).unwrap(); let res = rt
// assert_eq!(res[0], "name".to_owned()); .block_on(
// assert_eq!(res[1], "32".to_owned()); <(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");
// #[test] let () = <()>::from_request(&mut req).unwrap();
// fn test_extract_path_single() { }
// let mut router = Router::<()>::default(); }
// router.register_resource(Resource::new(ResourceDef::new("/{value}/")));
// let req = TestRequest::with_uri("/32/").finish();
// let info = router.recognize(&req, &(), 0);
// let req = req.with_route_info(info);
// assert_eq!(*Path::<i8>::from_request(&req, &()).unwrap(), 32);
// }
// #[test]
// fn test_tuple_extract() {
// let mut router = Router::<()>::default();
// router.register_resource(Resource::new(ResourceDef::new("/{key}/{value}/")));
// let req = TestRequest::with_uri("/name/user1/?id=test").finish();
// let info = router.recognize(&req, &(), 0);
// let req = req.with_route_info(info);
// let res = match <(Path<(String, String)>,)>::extract(&req).poll() {
// Ok(Async::Ready(res)) => res,
// _ => panic!("error"),
// };
// assert_eq!((res.0).0, "name");
// assert_eq!((res.0).1, "user1");
// let res = match <(Path<(String, String)>, Path<(String, String)>)>::extract(&req)
// .poll()
// {
// Ok(Async::Ready(res)) => res,
// _ => panic!("error"),
// };
// 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 () = <()>::extract(&req);
// }
// }

View File

@ -185,6 +185,11 @@ impl<P> ServiceFromRequest<P> {
self.req self.req
} }
#[inline]
pub fn match_info_mut(&mut self) -> &mut Path<Url> {
&mut self.req.path
}
/// Create service response for error /// Create service response for error
#[inline] #[inline]
pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse { pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse {

View File

@ -118,6 +118,12 @@ impl TestRequest {
self self
} }
/// Set request config
pub fn config<T: 'static>(mut self, data: T) -> Self {
self.extensions.insert(data);
self
}
/// Complete request creation and generate `ServiceRequest` instance /// Complete request creation and generate `ServiceRequest` instance
pub fn finish(mut self) -> ServiceRequest<PayloadStream> { pub fn finish(mut self) -> ServiceRequest<PayloadStream> {
let req = self.req.finish(); let req = self.req.finish();