mirror of
https://github.com/actix/actix-web.git
synced 2025-01-02 05:18:44 +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:
parent
205a964d8f
commit
fbead137f0
2 changed files with 24 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
||||||
at successful login to cycle a session (new key/cookie but keeps state).
|
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
|
Use ``Session.purge()`` at logout to invalid a session cookie (and remove
|
||||||
from redis cache, if applicable).
|
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
|
## [0.1.1] - 2019-06-03
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,9 @@ use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
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 actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
|
||||||
use futures::future::{ok, Ready};
|
use futures::future::{ok, Ready};
|
||||||
use serde::de::DeserializeOwned;
|
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)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
pub enum SessionStatus {
|
pub enum SessionStatus {
|
||||||
Changed,
|
Changed,
|
||||||
|
@ -281,6 +289,20 @@ mod tests {
|
||||||
assert_eq!(res, Some("value".to_string()));
|
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]
|
#[test]
|
||||||
fn purge_session() {
|
fn purge_session() {
|
||||||
let req = test::TestRequest::default().to_srv_request();
|
let req = test::TestRequest::default().to_srv_request();
|
||||||
|
|
Loading…
Reference in a new issue