mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
simplify application creation
This commit is contained in:
@ -11,20 +11,13 @@ use tokio_core::net::TcpListener;
|
||||
use actix::*;
|
||||
use actix_web::*;
|
||||
|
||||
|
||||
fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
||||
HttpServer::new(
|
||||
vec![Application::default("/")
|
||||
.resource("/", |r|
|
||||
r.route().method(Method::GET).h(httpcodes::HTTPOk))
|
||||
.finish()])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serve() {
|
||||
thread::spawn(|| {
|
||||
let sys = System::new("test");
|
||||
let srv = create_server();
|
||||
let srv = HttpServer::new(
|
||||
vec![Application::new("/")
|
||||
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]);
|
||||
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
|
||||
sys.run();
|
||||
});
|
||||
@ -42,7 +35,9 @@ fn test_serve_incoming() {
|
||||
thread::spawn(move || {
|
||||
let sys = System::new("test");
|
||||
|
||||
let srv = create_server();
|
||||
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.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
|
||||
sys.run();
|
||||
@ -89,19 +84,17 @@ fn test_middlewares() {
|
||||
let sys = System::new("test");
|
||||
|
||||
HttpServer::new(
|
||||
vec![Application::default("/")
|
||||
vec![Application::new("/")
|
||||
.middleware(MiddlewareTest{start: act_num1,
|
||||
response: act_num2,
|
||||
finish: act_num3})
|
||||
.resource("/", |r|
|
||||
r.route().method(Method::GET).h(httpcodes::HTTPOk))
|
||||
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))
|
||||
.finish()])
|
||||
.serve::<_, ()>("127.0.0.1:58904").unwrap();
|
||||
sys.run();
|
||||
});
|
||||
|
||||
assert!(reqwest::get("http://localhost:58904/").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);
|
||||
|
Reference in New Issue
Block a user