mirror of
https://github.com/actix/actix-web.git
synced 2024-12-22 16:16:40 +00:00
add TestRequest::run_async_result helper method
This commit is contained in:
parent
cc3fbd27e0
commit
bf7779a9a3
1 changed files with 20 additions and 1 deletions
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 a new issue