1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-27 18:49:01 +02:00

add TestRequest::run(), allows to run async functions

This commit is contained in:
Nikolay Kim
2019-04-12 11:28:57 -07:00
parent 3fb7343e73
commit b4768a8f81
2 changed files with 13 additions and 1 deletions

View File

@ -5,6 +5,8 @@ use actix_http::http::header::{Header, HeaderName, IntoHeaderValue};
use actix_http::http::{HttpTryFrom, Method, Uri, Version};
use actix_http::test::{TestBuffer, TestRequest as HttpTestRequest};
use actix_router::{Path, Url};
use actix_rt::Runtime;
use futures::IntoFuture;
use crate::{FramedRequest, State};
@ -114,6 +116,16 @@ impl<S> TestRequest<S> {
let framed = Framed::new(TestBuffer::empty(), Codec::default());
FramedRequest::new(req, framed, self.path, self.state)
}
/// This method generates `FramedRequest` instance and executes async handler
pub fn run<F, R, I, E>(self, f: F) -> Result<I, E>
where
F: FnOnce(FramedRequest<TestBuffer, S>) -> R,
R: IntoFuture<Item = I, Error = E>,
{
let mut rt = Runtime::new().unwrap();
rt.block_on(f(self.finish()).into_future())
}
}
#[cfg(test)]