From 542782f28a6a2c9215f63be27bd13d68a17b5523 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 8 Aug 2018 13:57:13 -0700 Subject: [PATCH 1/3] add HttpRequest::drop_state() --- src/httprequest.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/httprequest.rs b/src/httprequest.rs index 6f3bfe13..a21d772e 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -81,6 +81,15 @@ impl HttpRequest { } } + /// Construct new http request with empty state. + pub fn drop_state(&self) -> HttpRequest { + HttpRequest { + Rc::new(()), + req: self.req.as_ref().map(|r| r.clone()), + resource: self.resource.clone(), + } + } + #[inline] /// Construct new http request with new RouteInfo. pub(crate) fn with_route_info(&self, mut resource: ResourceInfo) -> HttpRequest { From b69774db61cef70ee08a024e1a7383dd93b6eb19 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 8 Aug 2018 14:23:16 -0700 Subject: [PATCH 2/3] fix attr name --- src/httprequest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index a21d772e..128dcbf1 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -84,7 +84,7 @@ impl HttpRequest { /// Construct new http request with empty state. pub fn drop_state(&self) -> HttpRequest { HttpRequest { - Rc::new(()), + state: Rc::new(()), req: self.req.as_ref().map(|r| r.clone()), resource: self.resource.clone(), } From cfe4829a56bc523afc7df8ca6314a52c35d01bc2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 8 Aug 2018 16:13:45 -0700 Subject: [PATCH 3/3] add TestRequest::execute() helper method --- src/extractor.rs | 6 ++++++ src/router.rs | 13 ------------- src/test.rs | 32 +++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/extractor.rs b/src/extractor.rs index 312287e0..233ad6ce 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -101,6 +101,12 @@ impl Path { } } +impl From for Path { + fn from(inner: T) -> Path { + Path{inner} + } +} + impl FromRequest for Path where T: DeserializeOwned, diff --git a/src/router.rs b/src/router.rs index 3d112bf6..ff52eac5 100644 --- a/src/router.rs +++ b/src/router.rs @@ -290,19 +290,6 @@ impl Router { } } - #[cfg(test)] - pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> ResourceInfo { - let mut params = Params::with_url(req.url()); - params.set_tail(prefix); - - ResourceInfo { - params, - prefix: 0, - rmap: self.rmap.clone(), - resource: ResourceId::Default, - } - } - #[cfg(test)] pub(crate) fn default_route_info(&self) -> ResourceInfo { ResourceInfo { diff --git a/src/test.rs b/src/test.rs index 70de5a16..42f51174 100644 --- a/src/test.rs +++ b/src/test.rs @@ -676,8 +676,6 @@ impl TestRequest { /// This method generates `HttpRequest` instance and runs handler /// with generated request. - /// - /// This method panics is handler returns actor or async result. pub fn run>(self, h: &H) -> Result { let req = self.finish(); let resp = h.handle(&req); @@ -686,7 +684,10 @@ impl TestRequest { Ok(resp) => match resp.into().into() { AsyncResultItem::Ok(resp) => Ok(resp), AsyncResultItem::Err(err) => Err(err), - AsyncResultItem::Future(_) => panic!("Async handler is not supported."), + AsyncResultItem::Future(fut) => { + let mut sys = System::new("test"); + sys.block_on(fut) + } }, Err(err) => Err(err.into()), } @@ -706,8 +707,8 @@ impl TestRequest { let req = self.finish(); let fut = h(req.clone()); - let mut core = Runtime::new().unwrap(); - match core.block_on(fut) { + let mut sys = System::new("test"); + match sys.block_on(fut) { Ok(r) => match r.respond_to(&req) { Ok(reply) => match reply.into().into() { AsyncResultItem::Ok(resp) => Ok(resp), @@ -718,4 +719,25 @@ impl TestRequest { Err(err) => Err(err), } } + + /// This method generates `HttpRequest` instance and executes handler + pub fn execute(self, f: F) -> Result + where F: FnOnce(&HttpRequest) -> R, + R: Responder + 'static, + { + let req = self.finish(); + let resp = f(&req); + + match resp.respond_to(&req) { + Ok(resp) => match resp.into().into() { + AsyncResultItem::Ok(resp) => Ok(resp), + AsyncResultItem::Err(err) => Err(err), + AsyncResultItem::Future(fut) => { + let mut sys = System::new("test"); + sys.block_on(fut) + } + }, + Err(err) => Err(err.into()), + } + } }