2017-10-16 22:13:32 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate tokio_core;
|
2017-10-22 21:48:43 +02:00
|
|
|
extern crate reqwest;
|
2017-10-16 22:13:32 +02:00
|
|
|
|
2017-10-23 02:33:24 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-10-16 22:13:32 +02:00
|
|
|
|
2017-10-23 02:33:24 +02:00
|
|
|
use actix_web::*;
|
2017-10-16 22:13:32 +02:00
|
|
|
|
|
|
|
#[test]
|
2017-12-27 01:47:55 +01:00
|
|
|
fn test_simple() {
|
2017-12-27 01:35:00 +01:00
|
|
|
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
|
|
|
|
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
|
2017-10-16 22:13:32 +02:00
|
|
|
}
|
2017-10-23 02:33:24 +02:00
|
|
|
|
2017-12-27 01:47:55 +01:00
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
|
2017-10-23 02:33:24 +02:00
|
|
|
struct MiddlewareTest {
|
|
|
|
start: Arc<AtomicUsize>,
|
|
|
|
response: Arc<AtomicUsize>,
|
|
|
|
finish: Arc<AtomicUsize>,
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:59:41 +01:00
|
|
|
impl<S> middleware::Middleware<S> for MiddlewareTest {
|
|
|
|
fn start(&self, _: &mut HttpRequest<S>) -> middleware::Started {
|
2017-10-23 02:33:24 +02:00
|
|
|
self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2017-12-27 04:59:41 +01:00
|
|
|
middleware::Started::Done
|
2017-10-23 02:33:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:59:41 +01:00
|
|
|
fn response(&self, _: &mut HttpRequest<S>, resp: HttpResponse) -> middleware::Response {
|
2017-10-23 02:33:24 +02:00
|
|
|
self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2017-12-27 04:59:41 +01:00
|
|
|
middleware::Response::Done(resp)
|
2017-10-23 02:33:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:59:41 +01:00
|
|
|
fn finish(&self, _: &mut HttpRequest<S>, _: &HttpResponse) -> middleware::Finished {
|
2017-10-23 02:33:24 +02:00
|
|
|
self.finish.store(self.finish.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2017-12-27 04:59:41 +01:00
|
|
|
middleware::Finished::Done
|
2017-10-23 02:33:24 +02: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);
|
|
|
|
|
2017-12-27 01:35:00 +01:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
|
2017-10-23 02:33:24 +02:00
|
|
|
assert_eq!(num1.load(Ordering::Relaxed), 1);
|
|
|
|
assert_eq!(num2.load(Ordering::Relaxed), 1);
|
|
|
|
assert_eq!(num3.load(Ordering::Relaxed), 1);
|
|
|
|
}
|