diff --git a/README.md b/README.md index 2f0a8355e..9502c6501 100644 --- a/README.md +++ b/README.md @@ -5,16 +5,14 @@ Actix web is a small, fast, down-to-earth, open source rust web framework. ```rust,ignore use actix_web::*; -fn index(req: HttpRequest) -> String -{ - format!("Hello {}!", - &req.match_info()["name"]) +fn index(req: HttpRequest) -> String { + format!("Hello {}!", &req.match_info()["name"]) } fn main() { - HttpServer::new(Application::new("/") - .resource("/{name}", - |r| r.method(Method::GET).f(index))) + HttpServer::new( + Application::new("/") + .resource("/{name}", |r| r.method(Method::GET).f(index))) .serve::<_, ()>("127.0.0.1:8080"); } ``` diff --git a/src/info.rs b/src/info.rs index a5a01c539..190ce0c5c 100644 --- a/src/info.rs +++ b/src/info.rs @@ -65,7 +65,7 @@ impl<'a> ConnectionInfo<'a> { if scheme.is_none() { scheme = req.uri().scheme_part().map(|a| a.as_str()); if scheme.is_none() { - if let Some(ref router) = req.router() { + if let Some(router) = req.router() { if router.server_settings().secure() { scheme = Some("https") } @@ -88,7 +88,7 @@ impl<'a> ConnectionInfo<'a> { if host.is_none() { host = req.uri().authority_part().map(|a| a.as_str()); if host.is_none() { - if let Some(ref router) = req.router() { + if let Some(router) = req.router() { host = Some(router.server_settings().host()); } } @@ -104,8 +104,7 @@ impl<'a> ConnectionInfo<'a> { remote = h.split(',').next().map(|v| v.trim()); } } - if remote.is_none() { - // get peeraddr from socketaddr + if remote.is_none() { // get peeraddr from socketaddr peer = req.peer_addr().map(|addr| format!("{}", addr)); } } diff --git a/src/pred.rs b/src/pred.rs index 2eebd040d..b760af280 100644 --- a/src/pred.rs +++ b/src/pred.rs @@ -146,3 +146,101 @@ impl Predicate for HeaderPredicate { false } } + +#[cfg(test)] +mod tests { + use super::*; + use std::str::FromStr; + use http::{Uri, Version, Method}; + use http::header::{self, HeaderMap}; + use payload::Payload; + + #[test] + fn test_header() { + let mut headers = HeaderMap::new(); + headers.insert(header::TRANSFER_ENCODING, + header::HeaderValue::from_static("chunked")); + let mut req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), + Version::HTTP_11, headers, Payload::empty()); + + let pred = Header("transfer-encoding", "chunked"); + assert!(pred.check(&mut req)); + + let pred = Header("transfer-encoding", "other"); + assert!(!pred.check(&mut req)); + } + + #[test] + fn test_methods() { + let mut req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + let mut req2 = HttpRequest::new( + Method::POST, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + + assert!(Get().check(&mut req)); + assert!(!Get().check(&mut req2)); + assert!(Post().check(&mut req2)); + assert!(!Post().check(&mut req)); + + let mut r = HttpRequest::new( + Method::PUT, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Put().check(&mut r)); + assert!(!Put().check(&mut req)); + + let mut r = HttpRequest::new( + Method::DELETE, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Delete().check(&mut r)); + assert!(!Delete().check(&mut req)); + + let mut r = HttpRequest::new( + Method::HEAD, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Head().check(&mut r)); + assert!(!Head().check(&mut req)); + + let mut r = HttpRequest::new( + Method::OPTIONS, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Options().check(&mut r)); + assert!(!Options().check(&mut req)); + + let mut r = HttpRequest::new( + Method::CONNECT, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Connect().check(&mut r)); + assert!(!Connect().check(&mut req)); + + let mut r = HttpRequest::new( + Method::PATCH, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Patch().check(&mut r)); + assert!(!Patch().check(&mut req)); + + let mut r = HttpRequest::new( + Method::TRACE, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + assert!(Trace().check(&mut r)); + assert!(!Trace().check(&mut req)); + } + + #[test] + fn test_preds() { + let mut r = HttpRequest::new( + Method::TRACE, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), Payload::empty()); + + assert!(Not(Get()).check(&mut r)); + assert!(!Not(Trace()).check(&mut r)); + + assert!(All(vec![Trace(), Trace()]).check(&mut r)); + assert!(!All(vec![Get(), Trace()]).check(&mut r)); + + assert!(Any(vec![Get(), Trace()]).check(&mut r)); + assert!(!Any(vec![Get(), Get()]).check(&mut r)); + } +}