mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 05:41:50 +01:00
server tests
This commit is contained in:
parent
71f7606baf
commit
26989f5591
@ -50,6 +50,7 @@ features = []
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
|
reqwest = "0.8"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
@ -12,6 +12,7 @@ use httpresponse::HttpResponse;
|
|||||||
use server::HttpHandler;
|
use server::HttpHandler;
|
||||||
|
|
||||||
|
|
||||||
|
/// Middleware definition
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
pub trait Middleware {
|
pub trait Middleware {
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ pub trait Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Method is called when handler returns response,
|
/// 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 {
|
fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> HttpResponse {
|
||||||
resp
|
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]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate futures;
|
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
|
extern crate reqwest;
|
||||||
|
|
||||||
use std::net;
|
use std::{net, thread};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::io::prelude::*;
|
|
||||||
use actix::*;
|
use actix::*;
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use futures::Future;
|
use tokio_core::net::TcpListener;
|
||||||
use tokio_core::net::{TcpStream, TcpListener};
|
|
||||||
|
|
||||||
|
|
||||||
fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
||||||
@ -24,41 +22,28 @@ fn create_server<T, A>() -> HttpServer<T, A, Application<()>> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serve() {
|
fn test_serve() {
|
||||||
let sys = System::new("test");
|
thread::spawn(|| {
|
||||||
|
let sys = System::new("test");
|
||||||
|
|
||||||
let srv = create_server();
|
let srv = create_server();
|
||||||
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
|
srv.serve::<_, ()>("127.0.0.1:58902").unwrap();
|
||||||
let addr = net::SocketAddr::from_str("127.0.0.1:58902").unwrap();
|
sys.run();
|
||||||
|
});
|
||||||
Arbiter::handle().spawn(
|
assert!(reqwest::get("http://localhost:58906/").unwrap().status().is_success());
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serve_incoming() {
|
fn test_serve_incoming() {
|
||||||
let sys = System::new("test");
|
thread::spawn(|| {
|
||||||
|
let sys = System::new("test");
|
||||||
|
|
||||||
let srv = create_server();
|
let srv = create_server();
|
||||||
let addr = net::SocketAddr::from_str("127.0.0.1:58906").unwrap();
|
let addr = net::SocketAddr::from_str("127.0.0.1:58906").unwrap();
|
||||||
let tcp = TcpListener::bind(&addr, Arbiter::handle()).unwrap();
|
let tcp = TcpListener::bind(&addr, Arbiter::handle()).unwrap();
|
||||||
srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
|
srv.serve_incoming::<_, ()>(tcp.incoming()).unwrap();
|
||||||
let addr = net::SocketAddr::from_str("127.0.0.1:58906").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…
x
Reference in New Issue
Block a user