2017-12-27 01:35:00 +01:00
|
|
|
//! Various helpers for Actix applications to use during testing.
|
2017-12-27 04:48:02 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::str::FromStr;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::{net, thread};
|
2017-12-27 01:35:00 +01:00
|
|
|
|
2018-12-11 06:15:07 +01:00
|
|
|
use actix::{Actor, Addr, System};
|
2018-06-01 18:36:16 +02:00
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
use cookie::Cookie;
|
|
|
|
use futures::Future;
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::header::HeaderName;
|
|
|
|
use http::{HeaderMap, HttpTryFrom, Method, Uri, Version};
|
|
|
|
use net2::TcpBuilder;
|
2018-05-25 06:03:16 +02:00
|
|
|
use tokio::runtime::current_thread::Runtime;
|
2017-12-27 01:35:00 +01:00
|
|
|
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-05-29 19:59:24 +02:00
|
|
|
use openssl::ssl::SslAcceptorBuilder;
|
2018-08-08 05:48:25 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
2018-07-29 08:43:04 +02:00
|
|
|
use rustls::ServerConfig;
|
2018-03-20 19:23:35 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use application::{App, HttpApplication};
|
2018-02-20 05:03:57 +01:00
|
|
|
use body::Binary;
|
2018-04-14 01:02:01 +02:00
|
|
|
use client::{ClientConnector, ClientRequest, ClientRequestBuilder};
|
2017-12-27 04:48:02 +01:00
|
|
|
use error::Error;
|
2018-08-10 03:58:14 +02:00
|
|
|
use handler::{AsyncResult, AsyncResultItem, Handler, Responder};
|
2018-03-06 09:43:25 +01:00
|
|
|
use header::{Header, IntoHeaderValue};
|
2018-07-04 17:01:27 +02:00
|
|
|
use httprequest::HttpRequest;
|
2018-04-14 01:02:01 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2017-12-27 04:59:41 +01:00
|
|
|
use middleware::Middleware;
|
2017-12-27 04:48:02 +01:00
|
|
|
use param::Params;
|
|
|
|
use payload::Payload;
|
2018-07-12 11:30:01 +02:00
|
|
|
use resource::Resource;
|
2018-04-14 01:02:01 +02:00
|
|
|
use router::Router;
|
2018-07-04 18:52:49 +02:00
|
|
|
use server::message::{Request, RequestPool};
|
|
|
|
use server::{HttpServer, IntoHttpHandler, ServerSettings};
|
2018-06-25 06:58:04 +02:00
|
|
|
use uri::Url as InnerUrl;
|
2018-04-14 01:02:01 +02:00
|
|
|
use ws;
|
2017-12-27 01:35:00 +01:00
|
|
|
|
|
|
|
/// The `TestServer` type.
|
|
|
|
///
|
|
|
|
/// `TestServer` is very simple test server that simplify process of writing
|
2018-01-15 22:47:25 +01:00
|
|
|
/// integration tests cases for actix web applications.
|
2017-12-27 01:35:00 +01:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// #
|
2018-06-25 06:58:04 +02:00
|
|
|
/// # fn my_handler(req: &HttpRequest) -> HttpResponse {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// # HttpResponse::Ok().into()
|
2017-12-27 01:35:00 +01:00
|
|
|
/// # }
|
|
|
|
/// #
|
|
|
|
/// # fn main() {
|
|
|
|
/// use actix_web::test::TestServer;
|
|
|
|
///
|
2018-02-19 22:18:18 +01:00
|
|
|
/// let mut srv = TestServer::new(|app| app.handler(my_handler));
|
2017-12-27 01:35:00 +01:00
|
|
|
///
|
2018-02-19 22:18:18 +01:00
|
|
|
/// let req = srv.get().finish().unwrap();
|
|
|
|
/// let response = srv.execute(req.send()).unwrap();
|
|
|
|
/// assert!(response.status().is_success());
|
2017-12-27 01:35:00 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub struct TestServer {
|
|
|
|
addr: net::SocketAddr,
|
2018-03-20 19:23:35 +01:00
|
|
|
ssl: bool,
|
2018-05-27 14:02:49 +02:00
|
|
|
conn: Addr<ClientConnector>,
|
2018-05-29 19:31:37 +02:00
|
|
|
rt: Runtime,
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestServer {
|
|
|
|
/// Start new test server
|
|
|
|
///
|
2018-01-15 22:47:25 +01:00
|
|
|
/// This method accepts configuration method. You can add
|
2017-12-27 01:35:00 +01:00
|
|
|
/// middlewares or set handlers for test application.
|
|
|
|
pub fn new<F>(config: F) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-09-09 19:51:30 +02:00
|
|
|
F: Clone + Send + 'static + Fn(&mut TestApp<()>),
|
2018-03-20 19:23:35 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
TestServerBuilder::new(|| ()).start(config)
|
2018-03-20 19:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create test server builder
|
2018-09-09 19:51:30 +02:00
|
|
|
pub fn build() -> TestServerBuilder<(), impl Fn() -> () + Clone + Send + 'static> {
|
2018-04-14 01:02:01 +02:00
|
|
|
TestServerBuilder::new(|| ())
|
2018-03-20 19:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create test server builder with specific state factory
|
|
|
|
///
|
|
|
|
/// This method can be used for constructing application state.
|
2018-05-06 18:07:30 +02:00
|
|
|
/// Also it can be used for external dependency initialization,
|
2018-03-20 19:23:35 +01:00
|
|
|
/// like creating sync actors for diesel integration.
|
2018-09-09 19:51:30 +02:00
|
|
|
pub fn build_with_state<S, F>(state: F) -> TestServerBuilder<S, F>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-09-09 19:51:30 +02:00
|
|
|
F: Fn() -> S + Clone + Send + 'static,
|
2018-04-14 01:02:01 +02:00
|
|
|
S: 'static,
|
2017-12-27 01:35:00 +01:00
|
|
|
{
|
2018-03-20 19:23:35 +01:00
|
|
|
TestServerBuilder::new(state)
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 01:47:55 +01:00
|
|
|
/// Start new test server with application factory
|
2018-09-09 23:33:45 +02:00
|
|
|
pub fn with_factory<F, H>(factory: F) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-09-09 23:33:45 +02:00
|
|
|
F: Fn() -> H + Send + Clone + 'static,
|
2018-04-14 01:02:01 +02:00
|
|
|
H: IntoHttpHandler + 'static,
|
2017-12-27 01:47:55 +01:00
|
|
|
{
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
// run server in separate thread
|
2018-05-30 01:32:39 +02:00
|
|
|
thread::spawn(move || {
|
2017-12-27 01:47:55 +01:00
|
|
|
let sys = System::new("actix-test-server");
|
2017-12-27 05:07:31 +01:00
|
|
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
2017-12-27 01:47:55 +01:00
|
|
|
let local_addr = tcp.local_addr().unwrap();
|
|
|
|
|
2018-09-09 23:33:45 +02:00
|
|
|
let _ = HttpServer::new(factory)
|
2018-06-16 22:58:56 +02:00
|
|
|
.disable_signals()
|
|
|
|
.listen(tcp)
|
2018-09-15 18:55:38 +02:00
|
|
|
.keep_alive(5)
|
2018-06-16 22:58:56 +02:00
|
|
|
.start();
|
|
|
|
|
|
|
|
tx.send((System::current(), local_addr, TestServer::get_conn()))
|
|
|
|
.unwrap();
|
|
|
|
sys.run();
|
2017-12-27 01:47:55 +01:00
|
|
|
});
|
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
let (system, addr, conn) = rx.recv().unwrap();
|
|
|
|
System::set_current(system);
|
2017-12-27 01:47:55 +01:00
|
|
|
TestServer {
|
2018-02-26 23:33:56 +01:00
|
|
|
addr,
|
2018-05-29 19:31:37 +02:00
|
|
|
conn,
|
2018-03-20 19:23:35 +01:00
|
|
|
ssl: false,
|
2018-05-29 19:31:37 +02:00
|
|
|
rt: Runtime::new().unwrap(),
|
2017-12-27 01:47:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:02:49 +02:00
|
|
|
fn get_conn() -> Addr<ClientConnector> {
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-03-20 19:23:35 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
2018-03-20 19:23:35 +01:00
|
|
|
|
|
|
|
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
|
|
|
builder.set_verify(SslVerifyMode::NONE);
|
|
|
|
ClientConnector::with_connector(builder.build()).start()
|
|
|
|
}
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(all(
|
|
|
|
feature = "rust-tls",
|
|
|
|
not(any(feature = "alpn", feature = "ssl"))
|
|
|
|
))]
|
2018-07-29 08:43:04 +02:00
|
|
|
{
|
|
|
|
use rustls::ClientConfig;
|
|
|
|
use std::fs::File;
|
2018-08-01 00:40:52 +02:00
|
|
|
use std::io::BufReader;
|
2018-07-29 08:43:04 +02:00
|
|
|
let mut config = ClientConfig::new();
|
|
|
|
let pem_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
|
2018-08-01 00:40:52 +02:00
|
|
|
config.root_store.add_pem_file(pem_file).unwrap();
|
2018-08-01 19:59:00 +02:00
|
|
|
ClientConnector::with_connector(config).start()
|
2018-07-29 08:43:04 +02:00
|
|
|
}
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(not(any(feature = "alpn", feature = "ssl", feature = "rust-tls")))]
|
2018-03-20 19:23:35 +01:00
|
|
|
{
|
|
|
|
ClientConnector::default().start()
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 05:07:31 +01:00
|
|
|
/// Get firat available unused address
|
|
|
|
pub fn unused_addr() -> net::SocketAddr {
|
2017-12-27 05:52:21 +01:00
|
|
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
2017-12-27 20:22:27 +01:00
|
|
|
let socket = TcpBuilder::new_v4().unwrap();
|
|
|
|
socket.bind(&addr).unwrap();
|
|
|
|
socket.reuse_address(true).unwrap();
|
|
|
|
let tcp = socket.to_tcp_listener().unwrap();
|
2017-12-27 05:07:31 +01:00
|
|
|
tcp.local_addr().unwrap()
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:49:46 +01:00
|
|
|
/// Construct test server url
|
|
|
|
pub fn addr(&self) -> net::SocketAddr {
|
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:35:00 +01:00
|
|
|
/// Construct test server url
|
|
|
|
pub fn url(&self, uri: &str) -> String {
|
|
|
|
if uri.starts_with('/') {
|
2018-04-14 01:02:01 +02:00
|
|
|
format!(
|
2018-07-29 08:43:04 +02:00
|
|
|
"{}://localhost:{}{}",
|
2018-04-29 18:09:08 +02:00
|
|
|
if self.ssl { "https" } else { "http" },
|
2018-07-29 08:43:04 +02:00
|
|
|
self.addr.port(),
|
2018-04-14 01:02:01 +02:00
|
|
|
uri
|
|
|
|
)
|
2017-12-27 01:35:00 +01:00
|
|
|
} else {
|
2018-04-14 01:02:01 +02:00
|
|
|
format!(
|
2018-07-29 08:43:04 +02:00
|
|
|
"{}://localhost:{}/{}",
|
2018-04-29 18:09:08 +02:00
|
|
|
if self.ssl { "https" } else { "http" },
|
2018-07-29 08:43:04 +02:00
|
|
|
self.addr.port(),
|
2018-04-14 01:02:01 +02:00
|
|
|
uri
|
|
|
|
)
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop http server
|
|
|
|
fn stop(&mut self) {
|
2018-06-14 08:37:19 +02:00
|
|
|
System::current().stop();
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
/// Execute future on current core
|
|
|
|
pub fn execute<F, I, E>(&mut self, fut: F) -> Result<I, E>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: Future<Item = I, Error = E>,
|
2018-01-31 00:13:33 +01:00
|
|
|
{
|
2018-05-29 19:31:37 +02:00
|
|
|
self.rt.block_on(fut)
|
2018-01-31 00:13:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 01:21:18 +02:00
|
|
|
/// Connect to websocket server at a given path
|
|
|
|
pub fn ws_at(
|
2018-08-01 19:59:00 +02:00
|
|
|
&mut self, path: &str,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
|
2018-08-01 01:21:18 +02:00
|
|
|
let url = self.url(path);
|
2018-05-29 19:31:37 +02:00
|
|
|
self.rt
|
|
|
|
.block_on(ws::Client::with_connector(url, self.conn.clone()).connect())
|
2018-01-31 00:13:33 +01:00
|
|
|
}
|
2018-02-19 22:18:18 +01:00
|
|
|
|
2018-08-01 01:21:18 +02:00
|
|
|
/// Connect to a websocket server
|
|
|
|
pub fn ws(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
|
|
|
|
self.ws_at("/")
|
|
|
|
}
|
2018-08-01 19:59:00 +02:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
/// Create `GET` request
|
|
|
|
pub fn get(&self) -> ClientRequestBuilder {
|
|
|
|
ClientRequest::get(self.url("/").as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `POST` request
|
|
|
|
pub fn post(&self) -> ClientRequestBuilder {
|
2018-05-23 18:07:42 +02:00
|
|
|
ClientRequest::post(self.url("/").as_str())
|
2018-02-19 22:18:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `HEAD` request
|
|
|
|
pub fn head(&self) -> ClientRequestBuilder {
|
|
|
|
ClientRequest::head(self.url("/").as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect to test http server
|
|
|
|
pub fn client(&self, meth: Method, path: &str) -> ClientRequestBuilder {
|
|
|
|
ClientRequest::build()
|
|
|
|
.method(meth)
|
2018-03-20 19:23:35 +01:00
|
|
|
.uri(self.url(path).as_str())
|
|
|
|
.with_connector(self.conn.clone())
|
|
|
|
.take()
|
2018-02-19 22:18:18 +01:00
|
|
|
}
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestServer {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 06:49:50 +02:00
|
|
|
/// An `TestServer` builder
|
|
|
|
///
|
|
|
|
/// This type can be used to construct an instance of `TestServer` through a
|
|
|
|
/// builder-like pattern.
|
2018-09-09 19:51:30 +02:00
|
|
|
pub struct TestServerBuilder<S, F>
|
|
|
|
where
|
|
|
|
F: Fn() -> S + Send + Clone + 'static,
|
|
|
|
{
|
|
|
|
state: F,
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-05-29 19:59:24 +02:00
|
|
|
ssl: Option<SslAcceptorBuilder>,
|
2018-07-29 08:43:04 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
2018-08-01 04:51:26 +02:00
|
|
|
rust_ssl: Option<ServerConfig>,
|
2018-03-20 19:23:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-09 19:51:30 +02:00
|
|
|
impl<S: 'static, F> TestServerBuilder<S, F>
|
|
|
|
where
|
|
|
|
F: Fn() -> S + Send + Clone + 'static,
|
|
|
|
{
|
2018-06-02 15:51:58 +02:00
|
|
|
/// Create a new test server
|
2018-09-09 19:51:30 +02:00
|
|
|
pub fn new(state: F) -> TestServerBuilder<S, F> {
|
2018-03-20 19:23:35 +01:00
|
|
|
TestServerBuilder {
|
2018-09-09 19:51:30 +02:00
|
|
|
state,
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-03-20 19:23:35 +01:00
|
|
|
ssl: None,
|
2018-08-01 04:51:26 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
|
|
|
rust_ssl: None,
|
2018-03-20 19:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-03-20 19:23:35 +01:00
|
|
|
/// Create ssl server
|
2018-05-29 19:59:24 +02:00
|
|
|
pub fn ssl(mut self, ssl: SslAcceptorBuilder) -> Self {
|
2018-03-20 19:23:35 +01:00
|
|
|
self.ssl = Some(ssl);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:43:04 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
2018-08-01 04:51:26 +02:00
|
|
|
/// Create rust tls server
|
|
|
|
pub fn rustls(mut self, ssl: ServerConfig) -> Self {
|
|
|
|
self.rust_ssl = Some(ssl);
|
2018-07-29 08:43:04 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:23:35 +01:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
/// Configure test application and run test server
|
2018-09-09 19:51:30 +02:00
|
|
|
pub fn start<C>(mut self, config: C) -> TestServer
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-09-09 19:51:30 +02:00
|
|
|
C: Fn(&mut TestApp<S>) + Clone + Send + 'static,
|
2018-03-20 19:23:35 +01:00
|
|
|
{
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2018-08-01 04:51:26 +02:00
|
|
|
let mut has_ssl = false;
|
|
|
|
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-08-01 04:51:26 +02:00
|
|
|
{
|
|
|
|
has_ssl = has_ssl || self.ssl.is_some();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "rust-tls")]
|
|
|
|
{
|
|
|
|
has_ssl = has_ssl || self.rust_ssl.is_some();
|
|
|
|
}
|
2018-03-20 19:23:35 +01:00
|
|
|
|
|
|
|
// run server in separate thread
|
2018-05-30 01:32:39 +02:00
|
|
|
thread::spawn(move || {
|
2018-08-01 04:51:26 +02:00
|
|
|
let addr = TestServer::unused_addr();
|
2018-03-20 19:23:35 +01:00
|
|
|
|
2018-06-16 22:58:56 +02:00
|
|
|
let sys = System::new("actix-test-server");
|
|
|
|
let state = self.state;
|
2018-09-09 23:33:45 +02:00
|
|
|
let mut srv = HttpServer::new(move || {
|
2018-06-16 22:58:56 +02:00
|
|
|
let mut app = TestApp::new(state());
|
|
|
|
config(&mut app);
|
2018-09-09 23:33:45 +02:00
|
|
|
app
|
2018-06-16 22:58:56 +02:00
|
|
|
}).workers(1)
|
2018-09-15 18:55:38 +02:00
|
|
|
.keep_alive(5)
|
2018-08-09 20:52:32 +02:00
|
|
|
.disable_signals();
|
2018-06-16 22:58:56 +02:00
|
|
|
|
2018-08-01 04:51:26 +02:00
|
|
|
tx.send((System::current(), addr, TestServer::get_conn()))
|
2018-06-16 22:58:56 +02:00
|
|
|
.unwrap();
|
|
|
|
|
2018-09-28 07:15:38 +02:00
|
|
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
2018-06-16 22:58:56 +02:00
|
|
|
{
|
|
|
|
let ssl = self.ssl.take();
|
|
|
|
if let Some(ssl) = ssl {
|
2018-08-01 04:51:26 +02:00
|
|
|
let tcp = net::TcpListener::bind(addr).unwrap();
|
2018-09-28 07:15:38 +02:00
|
|
|
srv = srv.listen_ssl(tcp, ssl).unwrap();
|
2018-06-16 22:58:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 04:51:26 +02:00
|
|
|
#[cfg(feature = "rust-tls")]
|
2018-06-16 22:58:56 +02:00
|
|
|
{
|
2018-08-01 04:51:26 +02:00
|
|
|
let ssl = self.rust_ssl.take();
|
|
|
|
if let Some(ssl) = ssl {
|
|
|
|
let tcp = net::TcpListener::bind(addr).unwrap();
|
2018-09-28 06:24:21 +02:00
|
|
|
srv = srv.listen_rustls(tcp, ssl);
|
2018-08-01 04:51:26 +02:00
|
|
|
}
|
2018-06-16 22:58:56 +02:00
|
|
|
}
|
2018-08-01 04:51:26 +02:00
|
|
|
if !has_ssl {
|
|
|
|
let tcp = net::TcpListener::bind(addr).unwrap();
|
|
|
|
srv = srv.listen(tcp);
|
|
|
|
}
|
|
|
|
srv.start();
|
|
|
|
|
2018-06-16 22:58:56 +02:00
|
|
|
sys.run();
|
2018-03-20 19:23:35 +01:00
|
|
|
});
|
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
let (system, addr, conn) = rx.recv().unwrap();
|
|
|
|
System::set_current(system);
|
2018-03-20 19:23:35 +01:00
|
|
|
TestServer {
|
|
|
|
addr,
|
2018-05-29 19:31:37 +02:00
|
|
|
conn,
|
2018-08-01 04:51:26 +02:00
|
|
|
ssl: has_ssl,
|
2018-05-29 19:31:37 +02:00
|
|
|
rt: Runtime::new().unwrap(),
|
2018-03-20 19:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 01:35:00 +01:00
|
|
|
|
|
|
|
/// Test application helper for testing request handlers.
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct TestApp<S = ()> {
|
2018-03-31 09:16:55 +02:00
|
|
|
app: Option<App<S>>,
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> TestApp<S> {
|
|
|
|
fn new(state: S) -> TestApp<S> {
|
2018-03-31 09:16:55 +02:00
|
|
|
let app = App::with_state(state);
|
2018-04-29 18:09:08 +02:00
|
|
|
TestApp { app: Some(app) }
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Register handler for "/"
|
2018-06-25 06:58:04 +02:00
|
|
|
pub fn handler<F, R>(&mut self, handler: F)
|
|
|
|
where
|
|
|
|
F: Fn(&HttpRequest<S>) -> R + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
{
|
|
|
|
self.app = Some(self.app.take().unwrap().resource("/", |r| r.f(handler)));
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Register middleware
|
|
|
|
pub fn middleware<T>(&mut self, mw: T) -> &mut TestApp<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
T: Middleware<S> + 'static,
|
2017-12-27 01:35:00 +01:00
|
|
|
{
|
|
|
|
self.app = Some(self.app.take().unwrap().middleware(mw));
|
|
|
|
self
|
|
|
|
}
|
2018-03-28 23:24:32 +02:00
|
|
|
|
|
|
|
/// Register resource. This method is similar
|
2018-03-31 09:16:55 +02:00
|
|
|
/// to `App::resource()` method.
|
2018-04-04 07:06:18 +02:00
|
|
|
pub fn resource<F, R>(&mut self, path: &str, f: F) -> &mut TestApp<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-07-12 11:30:01 +02:00
|
|
|
F: FnOnce(&mut Resource<S>) -> R + 'static,
|
2018-03-28 23:24:32 +02:00
|
|
|
{
|
|
|
|
self.app = Some(self.app.take().unwrap().resource(path, f));
|
|
|
|
self
|
|
|
|
}
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> IntoHttpHandler for TestApp<S> {
|
|
|
|
type Handler = HttpApplication<S>;
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
fn into_handler(mut self) -> HttpApplication<S> {
|
|
|
|
self.app.take().unwrap().into_handler()
|
2017-12-27 01:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<S: 'static> Iterator for TestApp<S> {
|
|
|
|
type Item = HttpApplication<S>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if let Some(mut app) = self.app.take() {
|
|
|
|
Some(app.finish())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 04:48:02 +01:00
|
|
|
|
|
|
|
/// Test `HttpRequest` builder
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate http;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use http::{header, StatusCode};
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// use actix_web::test::TestRequest;
|
|
|
|
///
|
2018-06-25 06:58:04 +02:00
|
|
|
/// fn index(req: &HttpRequest) -> HttpResponse {
|
2017-12-27 04:48:02 +01:00
|
|
|
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// HttpResponse::Ok().into()
|
2017-12-27 04:48:02 +01:00
|
|
|
/// } else {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// HttpResponse::BadRequest().into()
|
2017-12-27 04:48:02 +01:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let resp = TestRequest::with_header("content-type", "text/plain")
|
2018-06-21 19:21:28 +02:00
|
|
|
/// .run(&index)
|
2018-06-01 18:36:16 +02:00
|
|
|
/// .unwrap();
|
2017-12-27 04:48:02 +01:00
|
|
|
/// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
///
|
2018-06-21 19:21:28 +02:00
|
|
|
/// let resp = TestRequest::default().run(&index).unwrap();
|
2017-12-27 04:48:02 +01:00
|
|
|
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub struct TestRequest<S> {
|
|
|
|
state: S,
|
|
|
|
version: Version,
|
|
|
|
method: Method,
|
|
|
|
uri: Uri,
|
|
|
|
headers: HeaderMap,
|
2018-06-19 12:45:26 +02:00
|
|
|
params: Params,
|
2017-12-27 04:48:02 +01:00
|
|
|
cookies: Option<Vec<Cookie<'static>>>,
|
|
|
|
payload: Option<Payload>,
|
2018-07-15 12:24:22 +02:00
|
|
|
prefix: u16,
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestRequest<()> {
|
|
|
|
fn default() -> TestRequest<()> {
|
|
|
|
TestRequest {
|
|
|
|
state: (),
|
|
|
|
method: Method::GET,
|
|
|
|
uri: Uri::from_str("/").unwrap(),
|
|
|
|
version: Version::HTTP_11,
|
|
|
|
headers: HeaderMap::new(),
|
2017-12-28 04:19:28 +01:00
|
|
|
params: Params::new(),
|
2017-12-27 04:48:02 +01:00
|
|
|
cookies: None,
|
|
|
|
payload: None,
|
2018-07-15 12:24:22 +02:00
|
|
|
prefix: 0,
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestRequest<()> {
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Create TestRequest and set request uri
|
2017-12-27 04:48:02 +01:00
|
|
|
pub fn with_uri(path: &str) -> TestRequest<()> {
|
|
|
|
TestRequest::default().uri(path)
|
|
|
|
}
|
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
/// Create TestRequest and set header
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest<()> {
|
2018-03-06 04:28:42 +01:00
|
|
|
TestRequest::default().set(hdr)
|
|
|
|
}
|
|
|
|
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Create TestRequest and set header
|
2017-12-27 04:48:02 +01:00
|
|
|
pub fn with_header<K, V>(key: K, value: V) -> TestRequest<()>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
2017-12-27 04:48:02 +01:00
|
|
|
{
|
|
|
|
TestRequest::default().header(key, value)
|
|
|
|
}
|
2019-01-02 11:24:08 +01:00
|
|
|
|
|
|
|
/// Create TestRequest and set request cookie
|
|
|
|
pub fn with_cookie(cookie: Cookie<'static>) -> TestRequest<()> {
|
|
|
|
TestRequest::default().cookie(cookie)
|
|
|
|
}
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
impl<S: 'static> TestRequest<S> {
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Start HttpRequest build process with application state
|
|
|
|
pub fn with_state(state: S) -> TestRequest<S> {
|
|
|
|
TestRequest {
|
2018-02-26 23:33:56 +01:00
|
|
|
state,
|
2017-12-27 04:48:02 +01:00
|
|
|
method: Method::GET,
|
|
|
|
uri: Uri::from_str("/").unwrap(),
|
|
|
|
version: Version::HTTP_11,
|
|
|
|
headers: HeaderMap::new(),
|
2017-12-28 04:19:28 +01:00
|
|
|
params: Params::new(),
|
2017-12-27 04:48:02 +01:00
|
|
|
cookies: None,
|
|
|
|
payload: None,
|
2018-07-15 12:24:22 +02:00
|
|
|
prefix: 0,
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set HTTP version of this request
|
|
|
|
pub fn version(mut self, ver: Version) -> Self {
|
|
|
|
self.version = ver;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set HTTP method of this request
|
|
|
|
pub fn method(mut self, meth: Method) -> Self {
|
|
|
|
self.method = meth;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set HTTP Uri of this request
|
|
|
|
pub fn uri(mut self, path: &str) -> Self {
|
|
|
|
self.uri = Uri::from_str(path).unwrap();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-02 11:24:08 +01:00
|
|
|
/// set cookie of this request
|
|
|
|
pub fn cookie(mut self, cookie: Cookie<'static>) -> Self {
|
|
|
|
if self.cookies.is_none() {
|
|
|
|
let mut should_insert = true;
|
|
|
|
let old_cookies = self.cookies.as_mut().unwrap();
|
|
|
|
for old_cookie in old_cookies.iter() {
|
|
|
|
if old_cookie == &cookie {
|
|
|
|
should_insert = false
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if should_insert {
|
|
|
|
old_cookies.push(cookie);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
self.cookies = Some(vec![cookie]);
|
|
|
|
};
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
/// Set a header
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
2018-03-06 04:28:42 +01:00
|
|
|
if let Ok(value) = hdr.try_into() {
|
|
|
|
self.headers.append(H::name(), value);
|
2018-04-14 01:02:01 +02:00
|
|
|
return self;
|
2018-03-06 04:28:42 +01:00
|
|
|
}
|
|
|
|
panic!("Can not set header");
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Set a header
|
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
2017-12-27 04:48:02 +01:00
|
|
|
{
|
|
|
|
if let Ok(key) = HeaderName::try_from(key) {
|
2018-03-06 09:43:25 +01:00
|
|
|
if let Ok(value) = value.try_into() {
|
2017-12-27 04:48:02 +01:00
|
|
|
self.headers.append(key, value);
|
2018-04-14 01:02:01 +02:00
|
|
|
return self;
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Can not create header");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set request path pattern parameter
|
|
|
|
pub fn param(mut self, name: &'static str, value: &'static str) -> Self {
|
2018-06-19 12:45:26 +02:00
|
|
|
self.params.add_static(name, value);
|
2017-12-27 04:48:02 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-02-20 05:01:38 +01:00
|
|
|
/// Set request payload
|
2018-02-20 05:03:57 +01:00
|
|
|
pub fn set_payload<B: Into<Binary>>(mut self, data: B) -> Self {
|
|
|
|
let mut data = data.into();
|
2018-02-20 05:01:38 +01:00
|
|
|
let mut payload = Payload::empty();
|
2018-02-20 05:03:57 +01:00
|
|
|
payload.unread_data(data.take());
|
2018-02-20 05:01:38 +01:00
|
|
|
self.payload = Some(payload);
|
|
|
|
self
|
|
|
|
}
|
2018-03-02 04:12:59 +01:00
|
|
|
|
2018-07-15 12:24:22 +02:00
|
|
|
/// Set request's prefix
|
|
|
|
pub fn prefix(mut self, prefix: u16) -> Self {
|
|
|
|
self.prefix = prefix;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Complete request creation and generate `HttpRequest` instance
|
|
|
|
pub fn finish(self) -> HttpRequest<S> {
|
2018-04-14 01:02:01 +02:00
|
|
|
let TestRequest {
|
|
|
|
state,
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
version,
|
|
|
|
headers,
|
2018-07-15 11:12:21 +02:00
|
|
|
mut params,
|
2018-04-14 01:02:01 +02:00
|
|
|
cookies,
|
|
|
|
payload,
|
2018-07-15 12:24:22 +02:00
|
|
|
prefix,
|
2018-04-14 01:02:01 +02:00
|
|
|
} = self;
|
2018-08-01 00:40:52 +02:00
|
|
|
let router = Router::<()>::default();
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2018-07-04 18:52:49 +02:00
|
|
|
let pool = RequestPool::pool(ServerSettings::default());
|
|
|
|
let mut req = RequestPool::get(pool);
|
|
|
|
{
|
|
|
|
let inner = req.inner_mut();
|
|
|
|
inner.method = method;
|
|
|
|
inner.url = InnerUrl::new(uri);
|
|
|
|
inner.version = version;
|
|
|
|
inner.headers = headers;
|
|
|
|
*inner.payload.borrow_mut() = payload;
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
params.set_url(req.url().clone());
|
2018-07-15 12:24:22 +02:00
|
|
|
let mut info = router.route_info_params(0, params);
|
|
|
|
info.set_prefix(prefix);
|
2018-06-25 06:58:04 +02:00
|
|
|
|
2018-07-15 12:24:22 +02:00
|
|
|
let mut req = HttpRequest::new(req, Rc::new(state), info);
|
2018-05-15 19:09:48 +02:00
|
|
|
req.set_cookies(cookies);
|
2018-06-25 06:58:04 +02:00
|
|
|
req
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:57:57 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
/// Complete request creation and generate `HttpRequest` instance
|
2018-07-15 11:12:21 +02:00
|
|
|
pub(crate) fn finish_with_router(self, router: Router<S>) -> HttpRequest<S> {
|
2018-04-14 01:02:01 +02:00
|
|
|
let TestRequest {
|
|
|
|
state,
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
version,
|
|
|
|
headers,
|
2018-07-15 11:12:21 +02:00
|
|
|
mut params,
|
2018-04-14 01:02:01 +02:00
|
|
|
cookies,
|
|
|
|
payload,
|
2018-07-15 12:24:22 +02:00
|
|
|
prefix,
|
2018-04-14 01:02:01 +02:00
|
|
|
} = self;
|
2018-03-07 23:56:53 +01:00
|
|
|
|
2018-07-04 18:52:49 +02:00
|
|
|
let pool = RequestPool::pool(ServerSettings::default());
|
|
|
|
let mut req = RequestPool::get(pool);
|
|
|
|
{
|
|
|
|
let inner = req.inner_mut();
|
|
|
|
inner.method = method;
|
|
|
|
inner.url = InnerUrl::new(uri);
|
|
|
|
inner.version = version;
|
|
|
|
inner.headers = headers;
|
|
|
|
*inner.payload.borrow_mut() = payload;
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
params.set_url(req.url().clone());
|
2018-07-15 12:24:22 +02:00
|
|
|
let mut info = router.route_info_params(0, params);
|
|
|
|
info.set_prefix(prefix);
|
|
|
|
let mut req = HttpRequest::new(req, Rc::new(state), info);
|
2018-05-15 19:09:48 +02:00
|
|
|
req.set_cookies(cookies);
|
2018-06-25 06:58:04 +02:00
|
|
|
req
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request creation and generate server `Request` instance
|
|
|
|
pub fn request(self) -> Request {
|
|
|
|
let TestRequest {
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
version,
|
|
|
|
headers,
|
|
|
|
payload,
|
2018-07-04 17:01:27 +02:00
|
|
|
..
|
2018-06-25 06:58:04 +02:00
|
|
|
} = self;
|
2018-07-04 18:52:49 +02:00
|
|
|
|
|
|
|
let pool = RequestPool::pool(ServerSettings::default());
|
|
|
|
let mut req = RequestPool::get(pool);
|
|
|
|
{
|
|
|
|
let inner = req.inner_mut();
|
|
|
|
inner.method = method;
|
|
|
|
inner.url = InnerUrl::new(uri);
|
|
|
|
inner.version = version;
|
|
|
|
inner.headers = headers;
|
|
|
|
*inner.payload.borrow_mut() = payload;
|
|
|
|
}
|
2018-06-25 06:58:04 +02:00
|
|
|
req
|
2018-01-03 19:57:57 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// This method generates `HttpRequest` instance and runs handler
|
|
|
|
/// with generated request.
|
2018-06-21 19:06:23 +02:00
|
|
|
pub fn run<H: Handler<S>>(self, h: &H) -> Result<HttpResponse, Error> {
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = self.finish();
|
2018-06-25 06:58:04 +02:00
|
|
|
let resp = h.handle(&req);
|
2017-12-27 04:48:02 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
match resp.respond_to(&req) {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(resp) => match resp.into().into() {
|
2018-05-04 01:22:08 +02:00
|
|
|
AsyncResultItem::Ok(resp) => Ok(resp),
|
|
|
|
AsyncResultItem::Err(err) => Err(err),
|
2018-08-09 01:13:45 +02:00
|
|
|
AsyncResultItem::Future(fut) => {
|
|
|
|
let mut sys = System::new("test");
|
|
|
|
sys.block_on(fut)
|
|
|
|
}
|
2017-12-27 04:48:02 +01:00
|
|
|
},
|
2018-05-04 01:22:08 +02:00
|
|
|
Err(err) => Err(err.into()),
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method generates `HttpRequest` instance and runs handler
|
|
|
|
/// with generated request.
|
|
|
|
///
|
|
|
|
/// This method panics is handler returns actor.
|
|
|
|
pub fn run_async<H, R, F, E>(self, h: H) -> Result<HttpResponse, E>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
H: Fn(HttpRequest<S>) -> F + 'static,
|
|
|
|
F: Future<Item = R, Error = E> + 'static,
|
|
|
|
R: Responder<Error = E> + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
2017-12-27 04:48:02 +01:00
|
|
|
{
|
|
|
|
let req = self.finish();
|
|
|
|
let fut = h(req.clone());
|
|
|
|
|
2018-08-09 01:13:45 +02:00
|
|
|
let mut sys = System::new("test");
|
|
|
|
match sys.block_on(fut) {
|
2018-05-04 20:44:22 +02:00
|
|
|
Ok(r) => match r.respond_to(&req) {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(reply) => match reply.into().into() {
|
2018-05-04 01:22:08 +02:00
|
|
|
AsyncResultItem::Ok(resp) => Ok(resp),
|
2018-04-14 01:02:01 +02:00
|
|
|
_ => panic!("Nested async replies are not supported"),
|
|
|
|
},
|
|
|
|
Err(e) => Err(e),
|
2017-12-27 04:48:02 +01:00
|
|
|
},
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 01:13:45 +02:00
|
|
|
|
2018-08-10 03:58:14 +02:00
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 01:13:45 +02:00
|
|
|
/// This method generates `HttpRequest` instance and executes handler
|
|
|
|
pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>
|
2018-08-09 20:52:32 +02:00
|
|
|
where
|
|
|
|
F: FnOnce(&HttpRequest<S>) -> R,
|
|
|
|
R: Responder + 'static,
|
2018-08-09 01:13:45 +02:00
|
|
|
{
|
|
|
|
let req = self.finish();
|
|
|
|
let resp = f(&req);
|
|
|
|
|
|
|
|
match resp.respond_to(&req) {
|
|
|
|
Ok(resp) => match resp.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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|