Skip to content

Commit

Permalink
add cookie session test
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 17, 2019
1 parent b64851c commit 85b598a
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions actix-session/src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::collections::HashMap;
use std::rc::Rc;
use std::time::Duration;

use actix_service::{Service, Transform};
use actix_web::cookie::{Cookie, CookieJar, Key, SameSite};
Expand All @@ -27,7 +28,6 @@ use derive_more::{Display, From};
use futures::future::{ok, Future, FutureResult};
use futures::Poll;
use serde_json::error::Error as JsonError;
use time::Duration;

use crate::Session;

Expand Down Expand Up @@ -98,7 +98,7 @@ impl CookieSessionInner {
}

if let Some(max_age) = self.max_age {
cookie.set_max_age(max_age);
cookie.set_max_age(time::Duration::from_std(max_age).unwrap());
}

if let Some(same_site) = self.same_site {
Expand Down Expand Up @@ -317,6 +317,7 @@ where
mod tests {
use super::*;
use actix_web::{test, web, App};
use bytes::Bytes;

#[test]
fn cookie_session() {
Expand All @@ -338,6 +339,26 @@ mod tests {
.is_some());
}

#[test]
fn private_cookie() {
let mut app = test::init_service(
App::new()
.wrap(CookieSession::private(&[0; 32]).secure(false))
.service(web::resource("/").to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
})),
);

let request = test::TestRequest::get().to_request();
let response = test::block_on(app.call(request)).unwrap();
assert!(response
.response()
.cookies()
.find(|c| c.name() == "actix-session")
.is_some());
}

#[test]
fn cookie_session_extractor() {
let mut app = test::init_service(
Expand All @@ -357,4 +378,44 @@ mod tests {
.find(|c| c.name() == "actix-session")
.is_some());
}

#[test]
fn basics() {
let mut app = test::init_service(
App::new()
.wrap(
CookieSession::signed(&[0; 32])
.path("/test/")
.name("actix-test")
.domain("localhost")
.http_only(true)
.same_site(SameSite::Lax)
.max_age(Duration::from_secs(100)),
)
.service(web::resource("/").to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
}))
.service(web::resource("/test/").to(|ses: Session| {
let val: usize = ses.get("counter").unwrap().unwrap();
format!("counter: {}", val)
})),
);

let request = test::TestRequest::get().to_request();
let response = test::block_on(app.call(request)).unwrap();
let cookie = response
.response()
.cookies()
.find(|c| c.name() == "actix-test")
.unwrap()
.clone();
assert_eq!(cookie.path().unwrap(), "/test/");

let request = test::TestRequest::with_uri("/test/")
.cookie(cookie)
.to_request();
let body = test::read_response(&mut app, request);
assert_eq!(body, Bytes::from_static(b"counter: 100"));
}
}

0 comments on commit 85b598a

Please sign in to comment.