From b4768a8f81dad8717362656d9f2962b1ea9a1448 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 12 Apr 2019 11:28:57 -0700 Subject: [PATCH] add TestRequest::run(), allows to run async functions --- actix-framed/Cargo.toml | 2 +- actix-framed/src/test.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/actix-framed/Cargo.toml b/actix-framed/Cargo.toml index a2919bcec..bbd7dd698 100644 --- a/actix-framed/Cargo.toml +++ b/actix-framed/Cargo.toml @@ -24,6 +24,7 @@ actix-codec = "0.1.2" actix-service = "0.3.6" actix-utils = "0.3.4" actix-router = "0.1.2" +actix-rt = "0.2.2" actix-http = { path = "../actix-http" } bytes = "0.4" @@ -31,7 +32,6 @@ futures = "0.1.25" log = "0.4" [dev-dependencies] -actix-rt = "0.2.2" actix-server = { version = "0.4.1", features=["ssl"] } actix-connect = { version = "0.1.0", features=["ssl"] } actix-http-test = { version = "0.1.0-alpha.3", features=["ssl"] } diff --git a/actix-framed/src/test.rs b/actix-framed/src/test.rs index 34a157279..3bc828df4 100644 --- a/actix-framed/src/test.rs +++ b/actix-framed/src/test.rs @@ -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 TestRequest { 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(self, f: F) -> Result + where + F: FnOnce(FramedRequest) -> R, + R: IntoFuture, + { + let mut rt = Runtime::new().unwrap(); + rt.block_on(f(self.finish()).into_future()) + } } #[cfg(test)]