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

feat: add access to UserSession from RequestHead (#1164)

* feat: add access to UserSession from RequestHead

* add test case for session from RequestHead and changes entry for the new feature
This commit is contained in:
tglman 2019-12-06 05:21:43 +00:00 committed by Nikolay Kim
parent 205a964d8f
commit fbead137f0
2 changed files with 24 additions and 1 deletions

View file

@ -6,6 +6,7 @@
at successful login to cycle a session (new key/cookie but keeps state).
Use ``Session.purge()`` at logout to invalid a session cookie (and remove
from redis cache, if applicable).
* Add access to the session from RequestHead for use of session from guard methods
## [0.1.1] - 2019-06-03

View file

@ -46,7 +46,9 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use actix_web::dev::{Extensions, Payload, ServiceRequest, ServiceResponse};
use actix_web::dev::{
Extensions, Payload, RequestHead, ServiceRequest, ServiceResponse,
};
use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
use futures::future::{ok, Ready};
use serde::de::DeserializeOwned;
@ -99,6 +101,12 @@ impl UserSession for ServiceRequest {
}
}
impl UserSession for RequestHead {
fn get_session(&mut self) -> Session {
Session::get_session(&mut *self.extensions_mut())
}
}
#[derive(PartialEq, Clone, Debug)]
pub enum SessionStatus {
Changed,
@ -281,6 +289,20 @@ mod tests {
assert_eq!(res, Some("value".to_string()));
}
#[test]
fn get_session_from_request_head() {
let mut req = test::TestRequest::default().to_srv_request();
Session::set_session(
vec![("key".to_string(), "\"value\"".to_string())].into_iter(),
&mut req,
);
let session = req.head_mut().get_session();
let res = session.get::<String>("key").unwrap();
assert_eq!(res, Some("value".to_string()));
}
#[test]
fn purge_session() {
let req = test::TestRequest::default().to_srv_request();