1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

simplify application creation

This commit is contained in:
Nikolay Kim
2017-12-06 11:00:39 -08:00
parent 87c7441f7d
commit c63f058647
26 changed files with 150 additions and 138 deletions

View File

@ -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);