1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/tests/test_server.rs

163 lines
5.0 KiB
Rust
Raw Normal View History

extern crate actix;
extern crate actix_web;
extern crate tokio_core;
2017-10-22 21:48:43 +02:00
extern crate reqwest;
2017-12-27 21:58:32 +01:00
extern crate futures;
2018-01-09 21:49:46 +01:00
extern crate h2;
extern crate http;
2017-12-28 18:11:25 +01:00
use std::{net, thread, time};
2017-12-27 20:22:27 +01:00
use std::sync::{Arc, mpsc};
2017-10-23 02:33:24 +02:00
use std::sync::atomic::{AtomicUsize, Ordering};
2017-12-27 21:58:32 +01:00
use futures::Future;
2018-01-09 21:49:46 +01:00
use h2::client;
use http::Request;
use tokio_core::net::TcpStream;
use tokio_core::reactor::Core;
2017-10-23 02:33:24 +02:00
use actix_web::*;
2017-12-27 05:07:31 +01:00
use actix::System;
#[test]
fn test_start() {
2017-12-27 20:22:27 +01:00
let _ = test::TestServer::unused_addr();
let (tx, rx) = mpsc::channel();
2017-12-27 05:07:31 +01:00
thread::spawn(move || {
let sys = System::new("test");
let srv = HttpServer::new(
|| vec![Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]);
2017-12-27 21:58:32 +01:00
2017-12-27 20:22:27 +01:00
let srv = srv.bind("127.0.0.1:0").unwrap();
2017-12-28 21:38:37 +01:00
let addr = srv.addrs()[0];
2017-12-27 21:58:32 +01:00
let srv_addr = srv.start();
let _ = tx.send((addr, srv_addr));
2017-12-27 05:07:31 +01:00
sys.run();
});
2017-12-27 21:58:32 +01:00
let (addr, srv_addr) = rx.recv().unwrap();
assert!(reqwest::get(&format!("http://{}/", addr)).unwrap().status().is_success());
// pause
let _ = srv_addr.call_fut(dev::PauseServer).wait();
2017-12-28 18:11:25 +01:00
thread::sleep(time::Duration::from_millis(100));
2017-12-27 21:58:32 +01:00
assert!(net::TcpStream::connect(addr).is_err());
// resume
let _ = srv_addr.call_fut(dev::ResumeServer).wait();
2017-12-27 05:07:31 +01:00
assert!(reqwest::get(&format!("http://{}/", addr)).unwrap().status().is_success());
}
#[test]
fn test_simple() {
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
assert!(reqwest::get(&srv.url("/")).unwrap().status().is_success());
}
2017-10-23 02:33:24 +02:00
2018-01-09 21:49:46 +01:00
#[test]
fn test_h2() {
let srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
let addr = srv.addr();
let mut core = Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = tcp.then(|res| {
client::handshake(res.unwrap())
}).then(move |res| {
let (mut client, h2) = res.unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response
});
let resp = core.run(tcp).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[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 {
2018-01-10 07:48:35 +01:00
fn start(&self, _: &mut HttpRequest<S>) -> Result<middleware::Started> {
2017-10-23 02:33:24 +02:00
self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2018-01-10 07:48:35 +01:00
Ok(middleware::Started::Done)
2017-10-23 02:33:24 +02:00
}
2018-01-10 07:48:35 +01:00
fn response(&self, _: &mut HttpRequest<S>, resp: HttpResponse) -> Result<middleware::Response> {
2017-10-23 02:33:24 +02:00
self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2018-01-10 07:48:35 +01:00
Ok(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);
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);
}
2018-01-10 05:00:18 +01:00
#[test]
fn test_resource_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);
let srv = test::TestServer::new(
move |app| app.handler2(
httpcodes::HTTPOk,
MiddlewareTest{start: Arc::clone(&act_num1),
response: Arc::clone(&act_num2),
finish: Arc::clone(&act_num3)})
);
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);
}