1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

add simple TestServer for integrational tests cases

This commit is contained in:
Nikolay Kim 2017-12-26 16:35:00 -08:00
parent e3b0f02794
commit f6510161b5
4 changed files with 176 additions and 51 deletions

View File

@ -117,6 +117,7 @@ pub mod httpcodes;
pub mod multipart;
pub mod middlewares;
pub mod pred;
pub mod test;
pub mod payload;
pub use error::{Error, Result, ResponseError};
pub use body::{Body, Binary};

View File

@ -191,7 +191,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
/// Get addresses of bound sockets.
pub fn addrs(&self) -> Vec<net::SocketAddr> {
self.sockets.keys().map(|addr| addr.clone()).collect()
self.sockets.keys().cloned().collect()
}
/// The socket address to bind

164
src/test/mod.rs Normal file
View File

@ -0,0 +1,164 @@
//! Various helpers for Actix applications to use during testing.
use std::{net, thread};
use std::sync::mpsc;
use actix::{Arbiter, SyncAddress, System, msgs};
use tokio_core::net::TcpListener;
use server::HttpServer;
use handler::Handler;
use channel::IntoHttpHandler;
use middlewares::Middleware;
use application::{Application, HttpApplication};
/// The `TestServer` type.
///
/// `TestServer` is very simple test server that simplify process of writing
/// integrational tests cases for actix web applications.
///
/// # Examples
///
/// ```rust
/// # extern crate actix;
/// # extern crate actix_web;
/// # use actix_web::*;
/// # extern crate reqwest;
/// #
/// # fn my_handler(req: HttpRequest) -> HttpResponse {
/// # httpcodes::HTTPOk.response()
/// # }
/// #
/// # fn main() {
/// use actix_web::test::TestServer;
///
/// let srv = TestServer::new(|app| app.handler(my_handler));
///
/// assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
/// # }
/// ```
pub struct TestServer {
addr: net::SocketAddr,
thread: Option<thread::JoinHandle<()>>,
sys: SyncAddress<System>,
}
impl TestServer {
/// Start new test server
///
/// This methos accepts configuration method. You can add
/// middlewares or set handlers for test application.
pub fn new<F>(config: F) -> Self
where F: Sync + Send + 'static + Fn(&mut TestApp<()>),
{
TestServer::with_state(||(), config)
}
/// Start new test server with custom application state
///
/// This methos accepts state factory and configuration method.
pub fn with_state<S, FS, F>(state: FS, config: F) -> Self
where S: 'static,
FS: Sync + Send + 'static + Fn() -> S,
F: Sync + Send + 'static + Fn(&mut TestApp<S>),
{
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(move || {
let mut app = TestApp::new(state());
config(&mut app);
app}
).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,
}
}
/// Construct test server url
pub fn url(&self, uri: &str) -> String {
if uri.starts_with('/') {
format!("http://{}{}", self.addr, uri)
} else {
format!("http://{}/{}", self.addr, uri)
}
}
/// Stop http server
fn stop(&mut self) {
if let Some(handle) = self.thread.take() {
self.sys.send(msgs::SystemExit(0));
let _ = handle.join();
}
}
}
impl Drop for TestServer {
fn drop(&mut self) {
self.stop()
}
}
/// Test application helper for testing request handlers.
pub struct TestApp<S=()> {
app: Option<Application<S>>,
}
impl<S: 'static> TestApp<S> {
fn new(state: S) -> TestApp<S> {
let app = Application::with_state(state);
TestApp{app: Some(app)}
}
/// Register handler for "/"
pub fn handler<H: Handler<S>>(&mut self, handler: H) {
self.app = Some(self.app.take().unwrap().resource("/", |r| r.h(handler)));
}
/// Register middleware
pub fn middleware<T>(&mut self, mw: T) -> &mut TestApp<S>
where T: Middleware<S> + 'static
{
self.app = Some(self.app.take().unwrap().middleware(mw));
self
}
}
impl<S: 'static> IntoHttpHandler for TestApp<S> {
type Handler = HttpApplication<S>;
fn into_handler(self) -> HttpApplication<S> {
self.app.unwrap().finish()
}
}
#[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
}
}
}

View File

@ -3,48 +3,15 @@ extern crate actix_web;
extern crate tokio_core;
extern crate reqwest;
use std::{net, thread};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio_core::net::TcpListener;
use actix::*;
use actix_web::*;
#[test]
fn test_serve() {
thread::spawn(|| {
let sys = System::new("test");
let srv = HttpServer::new(
|| vec![Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]);
srv.bind("127.0.0.1:58902").unwrap().start();
sys.run();
});
assert!(reqwest::get("http://localhost:58902/").unwrap().status().is_success());
}
#[test]
fn test_serve_incoming() {
let loopback = net::Ipv4Addr::new(127, 0, 0, 1);
let socket = net::SocketAddrV4::new(loopback, 0);
let tcp = net::TcpListener::bind(socket).unwrap();
let addr1 = tcp.local_addr().unwrap();
let addr2 = tcp.local_addr().unwrap();
thread::spawn(move || {
let sys = System::new("test");
let srv = HttpServer::new(
|| Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk)));
let tcp = TcpListener::from_listener(tcp, &addr2, Arbiter::handle()).unwrap();
srv.start_incoming(tcp.incoming(), false);
sys.run();
});
assert!(reqwest::get(&format!("http://{}/", addr1))
.unwrap().status().is_success());
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
}
struct MiddlewareTest {
@ -80,21 +47,14 @@ fn test_middlewares() {
let act_num2 = Arc::clone(&num2);
let act_num3 = Arc::clone(&num3);
thread::spawn(move || {
let sys = System::new("test");
let srv = test::TestServer::new(
move |app| app.middleware(MiddlewareTest{start: Arc::clone(&act_num1),
response: Arc::clone(&act_num2),
finish: Arc::clone(&act_num3)})
.handler(httpcodes::HTTPOk)
);
HttpServer::new(
move || vec![Application::new()
.middleware(MiddlewareTest{start: Arc::clone(&act_num1),
response: Arc::clone(&act_num2),
finish: Arc::clone(&act_num3)})
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))])
.bind("127.0.0.1:58904").unwrap()
.start();
sys.run();
});
assert!(reqwest::get("http://localhost:58904/").unwrap().status().is_success());
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
assert_eq!(num1.load(Ordering::Relaxed), 1);
assert_eq!(num2.load(Ordering::Relaxed), 1);
assert_eq!(num3.load(Ordering::Relaxed), 1);