1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-24 17:10:33 +00:00
actix-web/tests/test_server.rs

113 lines
3.4 KiB
Rust
Raw Normal View History

extern crate actix;
extern crate actix_web;
extern crate tokio_core;
2017-10-22 19:48:43 +00:00
extern crate reqwest;
2017-10-22 19:48:43 +00:00
use std::{net, thread};
2017-10-23 00:33:24 +00:00
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
2017-10-22 19:48:43 +00:00
use tokio_core::net::TcpListener;
2017-10-23 00:33:24 +00:00
use actix::*;
use actix_web::*;
2017-11-10 06:08:54 +00:00
2017-10-22 01:54:24 +00:00
fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
HttpServer::new(
2017-10-22 01:54:24 +00:00
vec![Application::default("/")
.resource("/", |r|
r.handler(Method::GET, |_, _, _| {
2017-10-30 04:39:59 +00:00
Ok(httpcodes::HTTPOk)
2017-10-22 01:54:24 +00:00
}))
.finish()])
}
#[test]
fn test_serve() {
2017-10-22 19:48:43 +00:00
thread::spawn(|| {
let sys = System::new("test");
let srv = create_server();
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
sys.run();
});
2017-10-23 00:47:22 +00:00
assert!(reqwest::get("http://localhost:58902/").unwrap().status().is_success());
}
#[test]
fn test_serve_incoming() {
2017-10-23 05:23:38 +00:00
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 || {
2017-10-22 19:48:43 +00:00
let sys = System::new("test");
2017-10-22 19:48:43 +00:00
let srv = create_server();
2017-10-23 05:23:38 +00:00
let tcp = TcpListener::from_listener(tcp, &addr2, Arbiter::handle()).unwrap();
2017-10-22 19:48:43 +00:00
srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
sys.run();
});
2017-10-23 05:23:38 +00:00
assert!(reqwest::get(&format!("http://{}/", addr1))
.unwrap().status().is_success());
}
2017-10-23 00:33:24 +00:00
struct MiddlewareTest {
start: Arc<AtomicUsize>,
response: Arc<AtomicUsize>,
finish: Arc<AtomicUsize>,
}
2017-11-10 06:08:54 +00:00
impl middlewares::Middleware for MiddlewareTest {
fn start(&self, _: &mut HttpRequest) -> middlewares::Started {
2017-10-23 00:33:24 +00:00
self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2017-11-10 06:08:54 +00:00
middlewares::Started::Done
2017-10-23 00:33:24 +00:00
}
2017-11-10 06:08:54 +00:00
fn response(&self, _: &mut HttpRequest, resp: HttpResponse) -> middlewares::Response {
2017-10-23 00:33:24 +00:00
self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2017-11-10 06:08:54 +00:00
middlewares::Response::Response(resp)
2017-10-23 00:33:24 +00:00
}
2017-11-10 06:08:54 +00:00
fn finish(&self, _: &mut HttpRequest, _: &HttpResponse) -> middlewares::Finished {
2017-10-23 00:33:24 +00:00
self.finish.store(self.finish.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2017-11-10 06:08:54 +00:00
middlewares::Finished::Done
2017-10-23 00:33:24 +00:00
}
}
#[test]
fn test_middlewares() {
let num1 = Arc::new(AtomicUsize::new(0));
let num2 = Arc::new(AtomicUsize::new(0));
let num3 = Arc::new(AtomicUsize::new(0));
let act_num1 = Arc::clone(&num1);
let act_num2 = Arc::clone(&num2);
let act_num3 = Arc::clone(&num3);
thread::spawn(move || {
let sys = System::new("test");
HttpServer::new(
vec![Application::default("/")
.middleware(MiddlewareTest{start: act_num1,
response: act_num2,
finish: act_num3})
.resource("/", |r|
r.handler(Method::GET, |_, _, _| {
2017-10-30 04:39:59 +00:00
Ok(httpcodes::HTTPOk)
2017-10-23 00:33:24 +00:00
}))
.finish()])
2017-10-23 04:52:01 +00:00
.serve::<_, ()>("127.0.0.1:58904").unwrap();
2017-10-23 00:33:24 +00:00
sys.run();
});
2017-10-23 04:52:01 +00:00
assert!(reqwest::get("http://localhost:58904/").unwrap().status().is_success());
2017-10-23 00:33:24 +00:00
assert_eq!(num1.load(Ordering::Relaxed), 1);
assert_eq!(num2.load(Ordering::Relaxed), 1);
assert_eq!(num3.load(Ordering::Relaxed), 1);
}