1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-24 00:50:36 +00:00

add client cookie handling test

This commit is contained in:
Alex Whitney 2018-03-07 11:43:55 +00:00
parent 9afad5885b
commit 6cc3aaef1b

View file

@ -321,3 +321,62 @@ fn test_body_streaming_implicit() {
let bytes = srv.execute(response.body()).unwrap(); let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
#[test]
fn test_client_cookie_handling() {
use actix_web::header::Cookie;
fn err() -> Error {
use std::io::{ErrorKind, Error as IoError};
// stub some generic error
Error::from(IoError::from(ErrorKind::NotFound))
}
let cookie1 = Cookie::build("cookie1", "value1").finish();
let cookie2 = Cookie::build("cookie2", "value2")
.domain("www.example.org")
.path("/")
.secure(true)
.http_only(true)
.finish();
// Q: are all these clones really necessary? A: Yes, possibly
let cookie1b = cookie1.clone();
let cookie2b = cookie2.clone();
let mut srv = test::TestServer::new(
move |app| {
let cookie1 = cookie1b.clone();
let cookie2 = cookie2b.clone();
app.handler(move |req: HttpRequest| {
// Check cookies were sent correctly
req.cookie("cookie1").ok_or_else(err)
.and_then(|c1| if c1.value() == "value1" {
Ok(())
} else {
Err(err())
})
.and_then(|()| req.cookie("cookie2").ok_or_else(err))
.and_then(|c2| if c2.value() == "value2" {
Ok(())
} else {
Err(err())
})
// Send some cookies back
.map(|_|
httpcodes::HTTPOk.build()
.cookie(cookie1.clone())
.cookie(cookie2.clone())
.finish()
)
})
});
let request = srv.get()
.cookie(cookie1.clone())
.cookie(cookie2.clone())
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
let c1 = response.cookie("cookie1").expect("Missing cookie1");
assert_eq!(c1, &cookie1);
let c2 = response.cookie("cookie2").expect("Missing cookie2");
assert_eq!(c2, &cookie2);
}