2017-10-14 19:01:53 +02:00
|
|
|
extern crate actix_web;
|
|
|
|
extern crate http;
|
2017-10-14 19:40:58 +02:00
|
|
|
extern crate time;
|
2017-10-14 19:01:53 +02:00
|
|
|
|
2017-10-16 19:31:31 +02:00
|
|
|
use std::str;
|
2017-12-01 04:01:25 +01:00
|
|
|
use std::str::FromStr;
|
2017-12-08 01:22:26 +01:00
|
|
|
use std::collections::HashMap;
|
2017-11-29 04:49:17 +01:00
|
|
|
use actix_web::*;
|
2017-11-27 07:59:04 +01:00
|
|
|
use actix_web::dev::*;
|
2017-12-01 04:01:25 +01:00
|
|
|
use http::{header, Method, Version, HeaderMap, Uri};
|
2017-10-14 19:01:53 +02:00
|
|
|
|
|
|
|
|
2017-12-01 04:01:25 +01:00
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
|
|
|
|
HeaderMap::new(), Payload::empty());
|
|
|
|
let _ = format!("{:?}", req);
|
|
|
|
}
|
|
|
|
|
2017-10-14 19:01:53 +02:00
|
|
|
#[test]
|
|
|
|
fn test_no_request_cookies() {
|
|
|
|
let mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
2017-10-14 19:01:53 +02:00
|
|
|
assert!(req.cookies().is_empty());
|
|
|
|
let _ = req.load_cookies();
|
|
|
|
assert!(req.cookies().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 mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-14 19:01:53 +02:00
|
|
|
assert!(req.cookies().is_empty());
|
2017-10-14 20:13:02 +02:00
|
|
|
{
|
|
|
|
let cookies = req.load_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");
|
|
|
|
}
|
2017-10-14 20:02:25 +02:00
|
|
|
|
|
|
|
let cookie = req.cookie("cookie1");
|
|
|
|
assert!(cookie.is_some());
|
|
|
|
let cookie = cookie.unwrap();
|
|
|
|
assert_eq!(cookie.name(), "cookie1");
|
|
|
|
assert_eq!(cookie.value(), "value1");
|
2017-10-15 08:14:26 +02:00
|
|
|
|
|
|
|
let cookie = req.cookie("cookie-unknown");
|
|
|
|
assert!(cookie.is_none());
|
2017-10-14 19:01:53 +02:00
|
|
|
}
|
2017-10-14 19:40:58 +02:00
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
#[test]
|
|
|
|
fn test_no_request_range_header() {
|
2017-12-01 04:01:25 +01:00
|
|
|
let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
2017-10-15 07:52:38 +02:00
|
|
|
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"));
|
|
|
|
|
2017-12-01 04:01:25 +01:00
|
|
|
let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-15 07:52:38 +02:00
|
|
|
let ranges = req.range(100).unwrap();
|
|
|
|
assert_eq!(ranges.len(), 1);
|
|
|
|
assert_eq!(ranges[0].start, 0);
|
|
|
|
assert_eq!(ranges[0].length, 5);
|
|
|
|
}
|
2017-10-16 18:43:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_request_query() {
|
2017-12-01 04:01:25 +01:00
|
|
|
let req = HttpRequest::new(Method::GET, Uri::from_str("/?id=test").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
2017-10-16 18:43:10 +02:00
|
|
|
assert_eq!(req.query_string(), "id=test");
|
2017-10-29 14:03:21 +01:00
|
|
|
let query = req.query();
|
2017-10-29 23:04:44 +01:00
|
|
|
assert_eq!(&query["id"], "test");
|
2017-10-16 18:43:10 +02:00
|
|
|
}
|
2017-10-16 18:58:54 +02:00
|
|
|
|
|
|
|
#[test]
|
2017-10-16 19:20:16 +02:00
|
|
|
fn test_request_match_info() {
|
2017-12-01 04:01:25 +01:00
|
|
|
let mut req = HttpRequest::new(Method::GET, Uri::from_str("/value/?id=test").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
2017-10-16 18:58:54 +02:00
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
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());
|
2017-10-16 18:58:54 +02:00
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
assert_eq!(req.match_info().get("key"), Some("value"));
|
2017-10-16 18:58:54 +02:00
|
|
|
}
|
2017-10-16 19:31:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunked() {
|
|
|
|
let req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
2017-10-16 19:31:31 +02:00
|
|
|
assert!(!req.chunked().unwrap());
|
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::TRANSFER_ENCODING,
|
|
|
|
header::HeaderValue::from_static("chunked"));
|
|
|
|
let req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
|
|
|
|
headers, Payload::empty());
|
2017-10-16 19:31:31 +02:00
|
|
|
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(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-16 19:31:31 +02:00
|
|
|
assert!(req.chunked().is_err());
|
|
|
|
}
|