1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

tests for default predicates

This commit is contained in:
Nikolay Kim 2017-12-08 12:51:44 -08:00
parent 3e91b06241
commit 9043e7286d
3 changed files with 106 additions and 11 deletions

View File

@ -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");
}
```

View File

@ -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));
}
}

View File

@ -146,3 +146,101 @@ impl<S: 'static> Predicate<S> for HeaderPredicate<S> {
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));
}
}