mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 18:37:41 +02:00
fix doctest ci (#188)
This commit is contained in:
@ -69,16 +69,17 @@ impl CookieIdentityInner {
|
||||
value: Option<CookieValue>,
|
||||
) -> Result<()> {
|
||||
let add_cookie = value.is_some();
|
||||
let val = value.map(|val| {
|
||||
if !self.legacy_supported() {
|
||||
serde_json::to_string(&val)
|
||||
} else {
|
||||
Ok(val.identity)
|
||||
}
|
||||
});
|
||||
let val = value
|
||||
.map(|val| {
|
||||
if !self.legacy_supported() {
|
||||
serde_json::to_string(&val)
|
||||
} else {
|
||||
Ok(val.identity)
|
||||
}
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let mut cookie =
|
||||
Cookie::new(self.name.clone(), val.unwrap_or_else(|| Ok(String::new()))?);
|
||||
let mut cookie = Cookie::new(self.name.clone(), val.unwrap_or_default());
|
||||
cookie.set_path(self.path.clone());
|
||||
cookie.set_secure(self.secure);
|
||||
cookie.set_http_only(true);
|
||||
@ -108,10 +109,10 @@ impl CookieIdentityInner {
|
||||
};
|
||||
|
||||
if add_cookie {
|
||||
jar.private(&key).add(cookie);
|
||||
jar.private_mut(&key).add(cookie);
|
||||
} else {
|
||||
jar.add_original(cookie.clone());
|
||||
jar.private(&key).remove(cookie);
|
||||
jar.private_mut(&key).remove(cookie);
|
||||
}
|
||||
|
||||
for cookie in jar.delta() {
|
||||
@ -128,17 +129,19 @@ impl CookieIdentityInner {
|
||||
jar.add_original(cookie.clone());
|
||||
|
||||
let res = if self.legacy_supported() {
|
||||
jar.private(&self.key).get(&self.name).map(|n| CookieValue {
|
||||
identity: n.value().to_string(),
|
||||
login_timestamp: None,
|
||||
visit_timestamp: None,
|
||||
})
|
||||
jar.private_mut(&self.key)
|
||||
.get(&self.name)
|
||||
.map(|n| CookieValue {
|
||||
identity: n.value().to_string(),
|
||||
login_timestamp: None,
|
||||
visit_timestamp: None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
res.or_else(|| {
|
||||
jar.private(&self.key_v2)
|
||||
jar.private_mut(&self.key_v2)
|
||||
.get(&self.name)
|
||||
.and_then(|c| self.parse(c))
|
||||
})
|
||||
@ -391,7 +394,7 @@ mod tests {
|
||||
.copied()
|
||||
.collect();
|
||||
|
||||
jar.private(&Key::derive_from(&key)).add(Cookie::new(
|
||||
jar.private_mut(&Key::derive_from(&key)).add(Cookie::new(
|
||||
COOKIE_NAME,
|
||||
serde_json::to_string(&CookieValue {
|
||||
identity: identity.to_string(),
|
||||
@ -575,7 +578,7 @@ mod tests {
|
||||
|
||||
fn legacy_login_cookie(identity: &'static str) -> Cookie<'static> {
|
||||
let mut jar = CookieJar::new();
|
||||
jar.private(&Key::derive_from(&COOKIE_KEY_MASTER))
|
||||
jar.private_mut(&Key::derive_from(&COOKIE_KEY_MASTER))
|
||||
.add(Cookie::new(COOKIE_NAME, identity));
|
||||
jar.get(COOKIE_NAME).unwrap().clone()
|
||||
}
|
||||
@ -592,7 +595,7 @@ mod tests {
|
||||
cookies.add(Cookie::parse(cookie.to_str().unwrap().to_string()).unwrap());
|
||||
}
|
||||
let cookie = cookies
|
||||
.private(&Key::derive_from(&COOKIE_KEY_MASTER))
|
||||
.private_mut(&Key::derive_from(&COOKIE_KEY_MASTER))
|
||||
.get(COOKIE_NAME)
|
||||
.unwrap();
|
||||
assert_eq!(cookie.value(), identity);
|
||||
|
@ -1,10 +1,13 @@
|
||||
use std::rc::Rc;
|
||||
use std::{error::Error as StdError, rc::Rc};
|
||||
|
||||
use actix_web::{
|
||||
body::{AnyBody, MessageBody},
|
||||
dev::{Service, ServiceRequest, ServiceResponse, Transform},
|
||||
Error, HttpMessage, Result,
|
||||
};
|
||||
use futures_util::future::{ready, FutureExt, LocalBoxFuture, Ready};
|
||||
use futures_util::future::{
|
||||
ready, FutureExt as _, LocalBoxFuture, Ready, TryFutureExt as _,
|
||||
};
|
||||
|
||||
use crate::{identity::IdentityItem, IdentityPolicy};
|
||||
|
||||
@ -41,9 +44,10 @@ where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
T: IdentityPolicy,
|
||||
B: 'static,
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
{
|
||||
type Response = ServiceResponse<B>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = IdentityServiceMiddleware<S, T>;
|
||||
@ -73,12 +77,13 @@ impl<S, T> Clone for IdentityServiceMiddleware<S, T> {
|
||||
|
||||
impl<S, T, B> Service<ServiceRequest> for IdentityServiceMiddleware<S, T>
|
||||
where
|
||||
B: 'static,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
T: IdentityPolicy,
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
{
|
||||
type Response = ServiceResponse<B>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
@ -100,16 +105,19 @@ where
|
||||
|
||||
if let Some(id) = id {
|
||||
match backend.to_response(id.id, id.changed, &mut res).await {
|
||||
Ok(_) => Ok(res),
|
||||
Ok(_) => {
|
||||
Ok(res.map_body(|_, body| AnyBody::from_message(body)))
|
||||
}
|
||||
Err(e) => Ok(res.error_response(e)),
|
||||
}
|
||||
} else {
|
||||
Ok(res)
|
||||
Ok(res.map_body(|_, body| AnyBody::from_message(body)))
|
||||
}
|
||||
}
|
||||
Err(err) => Ok(req.error_response(err)),
|
||||
}
|
||||
}
|
||||
.map_ok(|res| res.map_body(|_, body| AnyBody::from_message(body)))
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user