mirror of
https://github.com/actix/actix-web.git
synced 2024-11-18 15:41:17 +00:00
allow to use application factory for test server
This commit is contained in:
parent
f6510161b5
commit
d3b7d2d6b3
2 changed files with 39 additions and 2 deletions
|
@ -8,7 +8,7 @@ use tokio_core::net::TcpListener;
|
||||||
|
|
||||||
use server::HttpServer;
|
use server::HttpServer;
|
||||||
use handler::Handler;
|
use handler::Handler;
|
||||||
use channel::IntoHttpHandler;
|
use channel::{HttpHandler, IntoHttpHandler};
|
||||||
use middlewares::Middleware;
|
use middlewares::Middleware;
|
||||||
use application::{Application, HttpApplication};
|
use application::{Application, HttpApplication};
|
||||||
|
|
||||||
|
@ -56,6 +56,36 @@ impl TestServer {
|
||||||
TestServer::with_state(||(), config)
|
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
|
/// Start new test server with custom application state
|
||||||
///
|
///
|
||||||
/// This methos accepts state factory and configuration method.
|
/// This methos accepts state factory and configuration method.
|
||||||
|
|
|
@ -9,11 +9,18 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serve() {
|
fn test_simple() {
|
||||||
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
|
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
|
||||||
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
|
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_application() {
|
||||||
|
let srv = test::TestServer::with_factory(
|
||||||
|
|| Application::new().resource("/", |r| r.h(httpcodes::HTTPOk)));
|
||||||
|
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
|
||||||
|
}
|
||||||
|
|
||||||
struct MiddlewareTest {
|
struct MiddlewareTest {
|
||||||
start: Arc<AtomicUsize>,
|
start: Arc<AtomicUsize>,
|
||||||
response: Arc<AtomicUsize>,
|
response: Arc<AtomicUsize>,
|
||||||
|
|
Loading…
Reference in a new issue