From bf7779a9a35c5b49f56904b644a6d033c2e59928 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 9 Aug 2018 18:58:14 -0700 Subject: [PATCH] add TestRequest::run_async_result helper method --- src/test.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/test.rs b/src/test.rs index 92aa6c8d..64aef663 100644 --- a/src/test.rs +++ b/src/test.rs @@ -26,7 +26,7 @@ use application::{App, HttpApplication}; use body::Binary; use client::{ClientConnector, ClientRequest, ClientRequestBuilder}; use error::Error; -use handler::{AsyncResultItem, Handler, Responder}; +use handler::{AsyncResult, AsyncResultItem, Handler, Responder}; use header::{Header, IntoHeaderValue}; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -722,6 +722,25 @@ impl TestRequest { } } + /// This method generates `HttpRequest` instance and executes handler + pub fn run_async_result(self, f: F) -> Result + where + F: FnOnce(&HttpRequest) -> R, + R: Into>, + { + let req = self.finish(); + let res = f(&req); + + match res.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) + } + } + } + /// This method generates `HttpRequest` instance and executes handler pub fn execute(self, f: F) -> Result where