mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 06:57:43 +02:00
allow to use application factory for test server
This commit is contained in:
@ -8,7 +8,7 @@ use tokio_core::net::TcpListener;
|
||||
|
||||
use server::HttpServer;
|
||||
use handler::Handler;
|
||||
use channel::IntoHttpHandler;
|
||||
use channel::{HttpHandler, IntoHttpHandler};
|
||||
use middlewares::Middleware;
|
||||
use application::{Application, HttpApplication};
|
||||
|
||||
@ -56,6 +56,36 @@ impl TestServer {
|
||||
TestServer::with_state(||(), config)
|
||||
}
|
||||
|
||||
/// Start new test server with application factory
|
||||
pub fn with_factory<H, F, U, V>(factory: F) -> Self
|
||||
where H: HttpHandler,
|
||||
F: Sync + Send + 'static + Fn() -> U,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
{
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
// run server in separate thread
|
||||
let join = thread::spawn(move || {
|
||||
let sys = System::new("actix-test-server");
|
||||
let tcp = net::TcpListener::bind("0.0.0.0:0").unwrap();
|
||||
let local_addr = tcp.local_addr().unwrap();
|
||||
let tcp = TcpListener::from_listener(tcp, &local_addr, Arbiter::handle()).unwrap();
|
||||
|
||||
HttpServer::new(factory).start_incoming(tcp.incoming(), false);
|
||||
|
||||
tx.send((Arbiter::system(), local_addr)).unwrap();
|
||||
let _ = sys.run();
|
||||
});
|
||||
|
||||
let (sys, addr) = rx.recv().unwrap();
|
||||
TestServer {
|
||||
addr: addr,
|
||||
thread: Some(join),
|
||||
sys: sys,
|
||||
}
|
||||
}
|
||||
|
||||
/// Start new test server with custom application state
|
||||
///
|
||||
/// This methos accepts state factory and configuration method.
|
||||
|
Reference in New Issue
Block a user