mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 18:37:41 +02:00
migrate to actix-web beta 14 (#209)
This commit is contained in:
@ -371,6 +371,7 @@ mod tests {
|
||||
use std::{borrow::Borrow, time::SystemTime};
|
||||
|
||||
use actix_web::{
|
||||
body::{BoxBody, EitherBody},
|
||||
cookie::{Cookie, CookieJar, Key, SameSite},
|
||||
dev::ServiceResponse,
|
||||
http::{header, StatusCode},
|
||||
@ -408,7 +409,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn assert_login_cookie(
|
||||
response: &mut ServiceResponse,
|
||||
response: &mut ServiceResponse<EitherBody<BoxBody>>,
|
||||
identity: &str,
|
||||
login_timestamp: LoginTimestampCheck,
|
||||
visit_timestamp: VisitTimeStampCheck,
|
||||
@ -577,13 +578,19 @@ mod tests {
|
||||
jar.get(COOKIE_NAME).unwrap().clone()
|
||||
}
|
||||
|
||||
async fn assert_logged_in(response: ServiceResponse, identity: Option<&str>) {
|
||||
async fn assert_logged_in(
|
||||
response: ServiceResponse<EitherBody<BoxBody>>,
|
||||
identity: Option<&str>,
|
||||
) {
|
||||
let bytes = test::read_body(response).await;
|
||||
let resp: Option<String> = serde_json::from_slice(&bytes[..]).unwrap();
|
||||
assert_eq!(resp.as_ref().map(|s| s.borrow()), identity);
|
||||
}
|
||||
|
||||
fn assert_legacy_login_cookie(response: &mut ServiceResponse, identity: &str) {
|
||||
fn assert_legacy_login_cookie(
|
||||
response: &mut ServiceResponse<EitherBody<BoxBody>>,
|
||||
identity: &str,
|
||||
) {
|
||||
let mut cookies = CookieJar::new();
|
||||
for cookie in response.headers().get_all(header::SET_COOKIE) {
|
||||
cookies.add(Cookie::parse(cookie.to_str().unwrap().to_string()).unwrap());
|
||||
@ -595,7 +602,7 @@ mod tests {
|
||||
assert_eq!(cookie.value(), identity);
|
||||
}
|
||||
|
||||
fn assert_no_login_cookie(response: &mut ServiceResponse) {
|
||||
fn assert_no_login_cookie(response: &mut ServiceResponse<EitherBody<BoxBody>>) {
|
||||
let mut cookies = CookieJar::new();
|
||||
for cookie in response.headers().get_all(header::SET_COOKIE) {
|
||||
cookies.add(Cookie::parse(cookie.to_str().unwrap().to_string()).unwrap());
|
||||
|
@ -1,8 +1,8 @@
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use actix_web::{
|
||||
dev::{Extensions, Payload},
|
||||
Error, FromRequest, HttpRequest,
|
||||
};
|
||||
use actix_utils::future::{ready, Ready};
|
||||
|
||||
pub(crate) struct IdentityItem {
|
||||
pub(crate) id: Option<String>,
|
||||
@ -48,12 +48,12 @@ impl Identity {
|
||||
/// Return the claimed identity of the user associated request or `None` if no identity can be
|
||||
/// found associated with the request.
|
||||
pub fn identity(&self) -> Option<String> {
|
||||
Identity::get_identity(&self.0.extensions())
|
||||
Identity::get_identity(&self.0.req_data())
|
||||
}
|
||||
|
||||
/// Remember identity.
|
||||
pub fn remember(&self, identity: String) {
|
||||
if let Some(id) = self.0.extensions_mut().get_mut::<IdentityItem>() {
|
||||
if let Some(id) = self.0.req_data_mut().get_mut::<IdentityItem>() {
|
||||
id.id = Some(identity);
|
||||
id.changed = true;
|
||||
}
|
||||
@ -61,7 +61,7 @@ impl Identity {
|
||||
|
||||
/// This method is used to 'forget' the current identity on subsequent requests.
|
||||
pub fn forget(&self) {
|
||||
if let Some(id) = self.0.extensions_mut().get_mut::<IdentityItem>() {
|
||||
if let Some(id) = self.0.req_data_mut().get_mut::<IdentityItem>() {
|
||||
id.id = None;
|
||||
id.changed = true;
|
||||
}
|
||||
|
@ -103,7 +103,11 @@ where
|
||||
mod tests {
|
||||
use std::time::SystemTime;
|
||||
|
||||
use actix_web::{dev::ServiceResponse, test, web, App, Error};
|
||||
use actix_web::{
|
||||
body::{BoxBody, EitherBody},
|
||||
dev::ServiceResponse,
|
||||
test, web, App, Error,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
@ -130,7 +134,7 @@ mod tests {
|
||||
f: F,
|
||||
) -> impl actix_service::Service<
|
||||
actix_http::Request,
|
||||
Response = ServiceResponse<actix_web::body::AnyBody>,
|
||||
Response = ServiceResponse<EitherBody<BoxBody>>,
|
||||
Error = Error,
|
||||
> {
|
||||
test::init_service(
|
||||
|
@ -2,11 +2,11 @@ use std::{error::Error as StdError, rc::Rc};
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use actix_web::{
|
||||
body::{AnyBody, MessageBody},
|
||||
body::{EitherBody, MessageBody},
|
||||
dev::{Service, ServiceRequest, ServiceResponse, Transform},
|
||||
Error, HttpMessage, Result,
|
||||
};
|
||||
use futures_util::future::{FutureExt as _, LocalBoxFuture, TryFutureExt as _};
|
||||
use futures_util::future::{FutureExt as _, LocalBoxFuture};
|
||||
|
||||
use crate::{identity::IdentityItem, IdentityPolicy};
|
||||
|
||||
@ -46,7 +46,7 @@ where
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Response = ServiceResponse<EitherBody<B>>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = IdentityServiceMiddleware<S, T>;
|
||||
@ -82,7 +82,7 @@ where
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Response = ServiceResponse<EitherBody<B>>;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
@ -104,17 +104,16 @@ where
|
||||
|
||||
if let Some(id) = id {
|
||||
match backend.to_response(id.id, id.changed, &mut res).await {
|
||||
Ok(_) => Ok(res.map_body(|_, body| AnyBody::new_boxed(body))),
|
||||
Err(e) => Ok(res.error_response(e)),
|
||||
Ok(_) => Ok(res.map_into_left_body()),
|
||||
Err(err) => Ok(res.error_response(err).map_into_right_body()),
|
||||
}
|
||||
} else {
|
||||
Ok(res.map_body(|_, body| AnyBody::new_boxed(body)))
|
||||
Ok(res.map_into_left_body())
|
||||
}
|
||||
}
|
||||
Err(err) => Ok(req.error_response(err)),
|
||||
Err(err) => Ok(req.error_response(err).map_into_right_body()),
|
||||
}
|
||||
}
|
||||
.map_ok(|res| res.map_body(|_, body| AnyBody::new_boxed(body)))
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user