mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 08:22:59 +01:00
add TestRequest::run_async_result helper method
This commit is contained in:
parent
cc3fbd27e0
commit
bf7779a9a3
21
src/test.rs
21
src/test.rs
@ -26,7 +26,7 @@ use application::{App, HttpApplication};
|
|||||||
use body::Binary;
|
use body::Binary;
|
||||||
use client::{ClientConnector, ClientRequest, ClientRequestBuilder};
|
use client::{ClientConnector, ClientRequest, ClientRequestBuilder};
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use handler::{AsyncResultItem, Handler, Responder};
|
use handler::{AsyncResult, AsyncResultItem, Handler, Responder};
|
||||||
use header::{Header, IntoHeaderValue};
|
use header::{Header, IntoHeaderValue};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
@ -722,6 +722,25 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This method generates `HttpRequest` instance and executes handler
|
||||||
|
pub fn run_async_result<F, R, I, E>(self, f: F) -> Result<I, E>
|
||||||
|
where
|
||||||
|
F: FnOnce(&HttpRequest<S>) -> R,
|
||||||
|
R: Into<AsyncResult<I, E>>,
|
||||||
|
{
|
||||||
|
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
|
/// This method generates `HttpRequest` instance and executes handler
|
||||||
pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>
|
pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>
|
||||||
where
|
where
|
||||||
|
Loading…
Reference in New Issue
Block a user