feat: rename max_age method which accepts `Duration` to max_age_time and add new max_age method accepting isize of seconds

This commit is contained in:
Kilerd Chan 2019-04-19 10:53:56 +08:00
parent e8217c99cb
commit 42f3308277
1 changed files with 36 additions and 4 deletions

View File

@ -427,11 +427,16 @@ impl CookieIdentityPolicy {
self
}
/// Sets the `max-age` field in the session cookie being built.
pub fn max_age(mut self, value: Duration) -> CookieIdentityPolicy {
/// Sets the `max-age` field in the session cookie being built with `chrono::Duration`.
pub fn max_age_time(mut self, value: Duration) -> CookieIdentityPolicy {
Rc::get_mut(&mut self.0).unwrap().max_age = Some(value);
self
}
/// Sets the `max-age` field in the session cookie being built with given number of seconds.
pub fn max_age(mut self, seconds: isize) -> CookieIdentityPolicy {
Rc::get_mut(&mut self.0).unwrap().max_age = Some(Duration::seconds(seconds as i64));
self
}
/// Sets the `same_site` field in the session cookie being built.
pub fn same_site(mut self, same_site: SameSite) -> Self {
@ -525,8 +530,9 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
assert!(resp.headers().contains_key(header::SET_COOKIE))
}
#[test]
fn test_identity_max_age() {
fn test_identity_max_age_time() {
let duration = Duration::days(1);
let mut srv = test::init_service(
App::new()
@ -535,7 +541,7 @@ mod tests {
.domain("www.rust-lang.org")
.name("actix_auth")
.path("/")
.max_age(duration)
.max_age_time(duration)
.secure(true),
))
.service(web::resource("/login").to(|id: Identity| {
@ -550,4 +556,30 @@ mod tests {
let c = resp.response().cookies().next().unwrap().to_owned();
assert_eq!(duration, c.max_age().unwrap());
}
#[test]
fn test_identity_max_age() {
let seconds = 60isize;
let mut srv = test::init_service(
App::new()
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32])
.domain("www.rust-lang.org")
.name("actix_auth")
.path("/")
.max_age(seconds)
.secure(true),
))
.service(web::resource("/login").to(|id: Identity| {
id.remember("test".to_string());
HttpResponse::Ok()
}))
);
let resp =
test::call_service(&mut srv, TestRequest::with_uri("/login").to_request());
assert_eq!(resp.status(), StatusCode::OK);
assert!(resp.headers().contains_key(header::SET_COOKIE));
let c = resp.response().cookies().next().unwrap().to_owned();
assert_eq!(Duration::seconds(seconds as i64), c.max_age().unwrap());
}
}