1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 10:27:42 +02:00

migrate to actix-web beta 14 (#209)

This commit is contained in:
Rob Ede
2021-12-11 16:05:21 +00:00
committed by GitHub
parent 700d90b68b
commit 74ec115161
27 changed files with 134 additions and 128 deletions

View File

@ -1,6 +1,9 @@
# Changes
## Unreleased - 2021-xx-xx
* Update `actix-web` dependency to `4.0.0.beta-14`. [#209]
[#209]: https://github.com/actix/actix-extras/pull/209
## 0.4.0-beta.4 - 2021-11-22

View File

@ -16,12 +16,13 @@ path = "src/lib.rs"
[dependencies]
actix-service = "2.0.0"
actix-utils = "3"
actix-web = { version = "4.0.0-beta.10", default-features = false, features = ["cookies", "secure-cookies"] }
actix-web = { version = "4.0.0-beta.14", default-features = false, features = ["cookies", "secure-cookies"] }
futures-util = { version = "0.3.7", default-features = false }
serde = "1.0"
serde_json = "1.0"
time = "0.2.23"
[dev-dependencies]
actix-http = "3.0.0-beta.11"
actix-http = "3.0.0-beta.15"
actix-rt = "2"

View File

@ -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());

View File

@ -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;
}

View File

@ -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(

View File

@ -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()
}
}