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

add session test

This commit is contained in:
Nikolay Kim 2018-05-02 19:11:44 -07:00
parent 3623383e83
commit 58079b5bbe
2 changed files with 30 additions and 3 deletions

View File

@ -247,7 +247,7 @@ impl<I, E> Reply<I, E> {
#[cfg(test)] #[cfg(test)]
pub(crate) fn as_msg(&self) -> &I { pub(crate) fn as_msg(&self) -> &I {
match self.0.as_ref().unwrap() { match self.0.as_ref().unwrap() {
ReplyResult::Ok(ref resp) => resp, &ReplyResult::Ok(ref resp) => resp,
_ => panic!(), _ => panic!(),
} }
} }
@ -255,7 +255,7 @@ impl<I, E> Reply<I, E> {
#[cfg(test)] #[cfg(test)]
pub(crate) fn as_err(&self) -> Option<&E> { pub(crate) fn as_err(&self) -> Option<&E> {
match self.0.as_ref().unwrap() { match self.0.as_ref().unwrap() {
ReplyResult::Err(ref err) => Some(err), &ReplyResult::Err(ref err) => Some(err),
_ => None, _ => None,
} }
} }

View File

@ -37,7 +37,7 @@
//! use actix_web::{server, App, HttpRequest, Result}; //! use actix_web::{server, App, HttpRequest, Result};
//! use actix_web::middleware::session::{RequestSession, SessionStorage, CookieSessionBackend}; //! use actix_web::middleware::session::{RequestSession, SessionStorage, CookieSessionBackend};
//! //!
//! fn index(mut req: HttpRequest) -> Result<&'static str> { //! fn index(req: HttpRequest) -> Result<&'static str> {
//! // access session data //! // access session data
//! if let Some(count) = req.session().get::<i32>("counter")? { //! if let Some(count) = req.session().get::<i32>("counter")? {
//! println!("SESSION value: {}", count); //! println!("SESSION value: {}", count);
@ -525,3 +525,30 @@ impl<S> SessionBackend<S> for CookieSessionBackend {
}) })
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use application::App;
use test;
#[test]
fn cookie_session() {
let mut srv = test::TestServer::with_factory(|| {
App::new()
.middleware(SessionStorage::new(
CookieSessionBackend::signed(&[0; 32]).secure(false),
))
.resource("/", |r| {
r.f(|req| {
let _ = req.session().set("counter", 100);
"test"
})
})
});
let request = srv.get().uri(srv.url("/")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.cookie("actix-session").is_some());
}
}