From 6b61041aec84dc05e81c087ac9d8cfcee0ad4bac Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 13 Dec 2017 11:16:26 -0800 Subject: [PATCH] move tests --- src/httprequest.rs | 112 ++++++++++++++++++++++++++++++++++ src/httpresponse.rs | 45 +++++++++++++- tests/test_httprequest.rs | 122 ------------------------------------- tests/test_httpresponse.rs | 41 ------------- 4 files changed, 155 insertions(+), 165 deletions(-) delete mode 100644 tests/test_httprequest.rs delete mode 100644 tests/test_httpresponse.rs diff --git a/src/httprequest.rs b/src/httprequest.rs index 7d5fbb740..4a5b1b0ee 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -532,6 +532,118 @@ mod tests { use router::Pattern; use resource::Resource; + #[test] + fn test_debug() { + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); + let dbg = format!("{:?}", req); + assert!(dbg.contains("HttpRequest")); + } + + #[test] + fn test_no_request_cookies() { + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); + assert!(req.cookies().unwrap().is_empty()); + } + + #[test] + fn test_request_cookies() { + let mut headers = HeaderMap::new(); + headers.insert(header::COOKIE, + header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); + + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); + { + let cookies = req.cookies().unwrap(); + assert_eq!(cookies.len(), 2); + assert_eq!(cookies[0].name(), "cookie1"); + assert_eq!(cookies[0].value(), "value1"); + assert_eq!(cookies[1].name(), "cookie2"); + assert_eq!(cookies[1].value(), "value2"); + } + + let cookie = req.cookie("cookie1"); + assert!(cookie.is_some()); + let cookie = cookie.unwrap(); + assert_eq!(cookie.name(), "cookie1"); + assert_eq!(cookie.value(), "value1"); + + let cookie = req.cookie("cookie-unknown"); + assert!(cookie.is_none()); + } + + #[test] + fn test_no_request_range_header() { + let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(), + Version::HTTP_11, HeaderMap::new(), None); + let ranges = req.range(100).unwrap(); + assert!(ranges.is_empty()); + } + + #[test] + fn test_request_range_header() { + let mut headers = HeaderMap::new(); + headers.insert(header::RANGE, + header::HeaderValue::from_static("bytes=0-4")); + + let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(), + Version::HTTP_11, headers, None); + let ranges = req.range(100).unwrap(); + assert_eq!(ranges.len(), 1); + assert_eq!(ranges[0].start, 0); + assert_eq!(ranges[0].length, 5); + } + + #[test] + fn test_request_query() { + let req = HttpRequest::new(Method::GET, Uri::from_str("/?id=test").unwrap(), + Version::HTTP_11, HeaderMap::new(), None); + assert_eq!(req.query_string(), "id=test"); + let query = req.query(); + assert_eq!(&query["id"], "test"); + } + + #[test] + fn test_request_match_info() { + let mut req = HttpRequest::new(Method::GET, Uri::from_str("/value/?id=test").unwrap(), + Version::HTTP_11, HeaderMap::new(), None); + + let mut resource = Resource::default(); + resource.name("index"); + let mut map = HashMap::new(); + map.insert(Pattern::new("index", "/{key}/"), Some(resource)); + let router = Router::new("", map); + assert!(router.recognize(&mut req).is_some()); + + assert_eq!(req.match_info().get("key"), Some("value")); + } + + #[test] + fn test_chunked() { + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); + assert!(!req.chunked().unwrap()); + + let mut headers = HeaderMap::new(); + headers.insert(header::TRANSFER_ENCODING, + header::HeaderValue::from_static("chunked")); + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); + assert!(req.chunked().unwrap()); + + let mut headers = HeaderMap::new(); + let s = unsafe{str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())}; + + headers.insert(header::TRANSFER_ENCODING, + header::HeaderValue::from_str(s).unwrap()); + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), + Version::HTTP_11, headers, None); + assert!(req.chunked().is_err()); + } + #[test] fn test_urlencoded_error() { let mut headers = HeaderMap::new(); diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 3ff4089c7..48b9e8877 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -600,7 +600,50 @@ impl FromRequest for BytesMut { #[cfg(test)] mod tests { use super::*; + use std::str::FromStr; + use time::Duration; + use http::{Method, Uri}; use body::Binary; + use {headers, httpcodes}; + + #[test] + fn test_debug() { + let resp = HttpResponse::Ok().finish().unwrap(); + let dbg = format!("{:?}", resp); + assert!(dbg.contains("HttpResponse")); + } + + #[test] + fn test_response_cookies() { + let mut headers = HeaderMap::new(); + headers.insert(header::COOKIE, + header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); + + let req = HttpRequest::new( + Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); + let cookies = req.cookies().unwrap(); + + let resp = httpcodes::HTTPOk + .build() + .cookie(headers::Cookie::build("name", "value") + .domain("www.rust-lang.org") + .path("/test") + .http_only(true) + .max_age(Duration::days(1)) + .finish()) + .del_cookie(&cookies[0]) + .body(Body::Empty); + + assert!(resp.is_ok()); + let resp = resp.unwrap(); + + let mut val: Vec<_> = resp.headers().get_all("Set-Cookie") + .iter().map(|v| v.to_str().unwrap().to_owned()).collect(); + val.sort(); + assert!(val[0].starts_with("cookie1=; Max-Age=0;")); + assert_eq!( + val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400"); + } #[test] fn test_basic_builder() { @@ -611,8 +654,6 @@ mod tests { .finish().unwrap(); assert_eq!(resp.version(), Some(Version::HTTP_10)); assert_eq!(resp.status(), StatusCode::NO_CONTENT); - - let _t = format!("{:?}", resp); } #[test] diff --git a/tests/test_httprequest.rs b/tests/test_httprequest.rs deleted file mode 100644 index aaaaf2764..000000000 --- a/tests/test_httprequest.rs +++ /dev/null @@ -1,122 +0,0 @@ -extern crate actix_web; -extern crate http; -extern crate time; - -use std::str; -use std::str::FromStr; -use std::collections::HashMap; -use actix_web::*; -use actix_web::dev::*; -use http::{header, Method, Version, HeaderMap, Uri}; - - -#[test] -fn test_debug() { - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); - let _ = format!("{:?}", req); -} - -#[test] -fn test_no_request_cookies() { - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); - assert!(req.cookies().unwrap().is_empty()); -} - -#[test] -fn test_request_cookies() { - let mut headers = HeaderMap::new(); - headers.insert(header::COOKIE, - header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); - - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); - { - let cookies = req.cookies().unwrap(); - assert_eq!(cookies.len(), 2); - assert_eq!(cookies[0].name(), "cookie1"); - assert_eq!(cookies[0].value(), "value1"); - assert_eq!(cookies[1].name(), "cookie2"); - assert_eq!(cookies[1].value(), "value2"); - } - - let cookie = req.cookie("cookie1"); - assert!(cookie.is_some()); - let cookie = cookie.unwrap(); - assert_eq!(cookie.name(), "cookie1"); - assert_eq!(cookie.value(), "value1"); - - let cookie = req.cookie("cookie-unknown"); - assert!(cookie.is_none()); -} - -#[test] -fn test_no_request_range_header() { - let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(), - Version::HTTP_11, HeaderMap::new(), None); - let ranges = req.range(100).unwrap(); - assert!(ranges.is_empty()); -} - -#[test] -fn test_request_range_header() { - let mut headers = HeaderMap::new(); - headers.insert(header::RANGE, - header::HeaderValue::from_static("bytes=0-4")); - - let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(), - Version::HTTP_11, headers, None); - let ranges = req.range(100).unwrap(); - assert_eq!(ranges.len(), 1); - assert_eq!(ranges[0].start, 0); - assert_eq!(ranges[0].length, 5); -} - -#[test] -fn test_request_query() { - let req = HttpRequest::new(Method::GET, Uri::from_str("/?id=test").unwrap(), - Version::HTTP_11, HeaderMap::new(), None); - assert_eq!(req.query_string(), "id=test"); - let query = req.query(); - assert_eq!(&query["id"], "test"); -} - -#[test] -fn test_request_match_info() { - let mut req = HttpRequest::new(Method::GET, Uri::from_str("/value/?id=test").unwrap(), - Version::HTTP_11, HeaderMap::new(), None); - - let mut resource = Resource::default(); - resource.name("index"); - let mut map = HashMap::new(); - map.insert(Pattern::new("index", "/{key}/"), Some(resource)); - let router = Router::new("", map); - assert!(router.recognize(&mut req).is_some()); - - assert_eq!(req.match_info().get("key"), Some("value")); -} - -#[test] -fn test_chunked() { - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, HeaderMap::new(), None); - assert!(!req.chunked().unwrap()); - - let mut headers = HeaderMap::new(); - headers.insert(header::TRANSFER_ENCODING, - header::HeaderValue::from_static("chunked")); - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); - assert!(req.chunked().unwrap()); - - let mut headers = HeaderMap::new(); - let s = unsafe{str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())}; - - headers.insert(header::TRANSFER_ENCODING, - header::HeaderValue::from_str(s).unwrap()); - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), - Version::HTTP_11, headers, None); - assert!(req.chunked().is_err()); -} diff --git a/tests/test_httpresponse.rs b/tests/test_httpresponse.rs deleted file mode 100644 index 922e07a95..000000000 --- a/tests/test_httpresponse.rs +++ /dev/null @@ -1,41 +0,0 @@ -extern crate actix_web; -extern crate http; -extern crate time; - -use actix_web::*; -use std::str::FromStr; -use time::Duration; -use http::{header, Method, Version, HeaderMap, Uri}; - - -#[test] -fn test_response_cookies() { - let mut headers = HeaderMap::new(); - headers.insert(header::COOKIE, - header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); - - let req = HttpRequest::new( - Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None); - let cookies = req.cookies().unwrap(); - - let resp = httpcodes::HTTPOk - .build() - .cookie(headers::Cookie::build("name", "value") - .domain("www.rust-lang.org") - .path("/test") - .http_only(true) - .max_age(Duration::days(1)) - .finish()) - .del_cookie(&cookies[0]) - .body(Body::Empty); - - assert!(resp.is_ok()); - let resp = resp.unwrap(); - - let mut val: Vec<_> = resp.headers().get_all("Set-Cookie") - .iter().map(|v| v.to_str().unwrap().to_owned()).collect(); - val.sort(); - assert!(val[0].starts_with("cookie1=; Max-Age=0;")); - assert_eq!( - val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400"); -}