1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00
actix-web/tests/test_httpmessage.rs

75 lines
2.3 KiB
Rust
Raw Normal View History

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
use actix_web::*;
2017-10-14 19:40:58 +02:00
use time::Duration;
2017-10-14 19:01:53 +02:00
use http::{header, Method, Uri, Version, HeaderMap, HttpTryFrom};
#[test]
fn test_no_request_cookies() {
let mut req = HttpRequest::new(
Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, HeaderMap::new());
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(
Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, headers);
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-14 19:01:53 +02:00
}
2017-10-14 19:40:58 +02:00
#[test]
fn test_response_cookies() {
let mut headers = HeaderMap::new();
headers.insert(header::COOKIE,
header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
let mut req = HttpRequest::new(
Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, headers);
let cookies = req.load_cookies().unwrap();
let resp = httpcodes::HTTPOk
.builder()
.cookie(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");
}