mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 12:00:38 +00:00
server tests
This commit is contained in:
parent
71f7606baf
commit
26989f5591
4 changed files with 23 additions and 36 deletions
|
@ -50,6 +50,7 @@ features = []
|
|||
|
||||
[dev-dependencies]
|
||||
env_logger = "*"
|
||||
reqwest = "0.8"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
|
@ -12,6 +12,7 @@ use httpresponse::HttpResponse;
|
|||
use server::HttpHandler;
|
||||
|
||||
|
||||
/// Middleware definition
|
||||
#[allow(unused_variables)]
|
||||
pub trait Middleware {
|
||||
|
||||
|
@ -21,7 +22,7 @@ pub trait Middleware {
|
|||
}
|
||||
|
||||
/// Method is called when handler returns response,
|
||||
/// but before sending body stream to peer.
|
||||
/// but before sending body streams to peer.
|
||||
fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> HttpResponse {
|
||||
resp
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Http framework for [Actix](https://github.com/fafhrd91/actix)
|
||||
//! Web framework for [Actix](https://github.com/fafhrd91/actix)
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate reqwest;
|
||||
|
||||
use std::net;
|
||||
use std::{net, thread};
|
||||
use std::str::FromStr;
|
||||
use std::io::prelude::*;
|
||||
use actix::*;
|
||||
use actix_web::*;
|
||||
use futures::Future;
|
||||
use tokio_core::net::{TcpStream, TcpListener};
|
||||
use tokio_core::net::TcpListener;
|
||||
|
||||
|
||||
fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
||||
|
@ -24,41 +22,28 @@ fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
|||
|
||||
#[test]
|
||||
fn test_serve() {
|
||||
let sys = System::new("test");
|
||||
thread::spawn(|| {
|
||||
let sys = System::new("test");
|
||||
|
||||
let srv = create_server();
|
||||
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
|
||||
let addr = net::SocketAddr::from_str("127.0.0.1:58902").unwrap();
|
||||
|
||||
Arbiter::handle().spawn(
|
||||
TcpStream::connect(&addr, Arbiter::handle()).and_then(|mut stream| {
|
||||
let _ = stream.write("GET /\r\n\r\n ".as_ref());
|
||||
Arbiter::system().send(msgs::SystemExit(0));
|
||||
futures::future::ok(())
|
||||
}).map_err(|_| panic!("should not happen"))
|
||||
);
|
||||
|
||||
sys.run();
|
||||
let srv = create_server();
|
||||
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
|
||||
sys.run();
|
||||
});
|
||||
assert!(reqwest::get("http://localhost:58906/").unwrap().status().is_success());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serve_incoming() {
|
||||
let sys = System::new("test");
|
||||
thread::spawn(|| {
|
||||
let sys = System::new("test");
|
||||
|
||||
let srv = create_server();
|
||||
let addr = net::SocketAddr::from_str("127.0.0.1:58906").unwrap();
|
||||
let tcp = TcpListener::bind(&addr, Arbiter::handle()).unwrap();
|
||||
srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
|
||||
let addr = net::SocketAddr::from_str("127.0.0.1:58906").unwrap();
|
||||
let srv = create_server();
|
||||
let addr = net::SocketAddr::from_str("127.0.0.1:58906").unwrap();
|
||||
let tcp = TcpListener::bind(&addr, Arbiter::handle()).unwrap();
|
||||
srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
|
||||
sys.run();
|
||||
|
||||
// connect
|
||||
Arbiter::handle().spawn(
|
||||
TcpStream::connect(&addr, Arbiter::handle()).and_then(|mut stream| {
|
||||
let _ = stream.write("GET /\r\n\r\n ".as_ref());
|
||||
Arbiter::system().send(msgs::SystemExit(0));
|
||||
futures::future::ok(())
|
||||
}).map_err(|_| panic!("should not happen"))
|
||||
);
|
||||
});
|
||||
|
||||
sys.run();
|
||||
assert!(reqwest::get("http://localhost:58906/").unwrap().status().is_success());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue