1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-22 14:55:56 +01:00

Fix clippy warnings

This commit is contained in:
Yuki Okushi 2020-07-06 15:47:22 +09:00
parent 6b839f0a30
commit bd963fb7d1
No known key found for this signature in database
GPG Key ID: B0986C85C0E2DAA1
4 changed files with 46 additions and 61 deletions

View File

@ -46,6 +46,9 @@
//! .service(web::resource("/logout.html").to(logout));
//! }
//! ```
#![allow(clippy::needless_doctest_main)]
use std::cell::RefCell;
use std::future::Future;
use std::rc::Rc;
@ -200,14 +203,12 @@ pub trait IdentityPolicy: Sized + 'static {
/// use actix_web::App;
/// use actix_identity::{CookieIdentityPolicy, IdentityService};
///
/// fn main() {
/// let app = App::new().wrap(IdentityService::new(
/// // <- create identity middleware
/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
/// .name("auth-cookie")
/// .secure(false),
/// ));
/// }
/// let app = App::new().wrap(IdentityService::new(
/// // <- create identity middleware
/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
/// .name("auth-cookie")
/// .secure(false),
/// ));
/// ```
pub struct IdentityService<T> {
backend: Rc<T>,
@ -427,16 +428,12 @@ impl CookieIdentityInner {
let value: CookieValue = serde_json::from_str(cookie.value()).ok()?;
let now = SystemTime::now();
if let Some(visit_deadline) = self.visit_deadline {
if now.duration_since(value.visit_timestamp?).ok()?
> visit_deadline
{
if now.duration_since(value.visit_timestamp?).ok()? > visit_deadline {
return None;
}
}
if let Some(login_deadline) = self.login_deadline {
if now.duration_since(value.login_timestamp?).ok()?
> login_deadline
{
if now.duration_since(value.login_timestamp?).ok()? > login_deadline {
return None;
}
}
@ -469,16 +466,14 @@ impl CookieIdentityInner {
/// use actix_web::App;
/// use actix_identity::{CookieIdentityPolicy, IdentityService};
///
/// fn main() {
/// let app = App::new().wrap(IdentityService::new(
/// // <- create identity middleware
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
/// .domain("www.rust-lang.org")
/// .name("actix_auth")
/// .path("/")
/// .secure(true),
/// ));
/// }
/// let app = App::new().wrap(IdentityService::new(
/// // <- create identity middleware
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
/// .domain("www.rust-lang.org")
/// .name("actix_auth")
/// .path("/")
/// .secure(true),
/// ));
/// ```
pub struct CookieIdentityPolicy(Rc<CookieIdentityInner>);
@ -557,11 +552,7 @@ impl IdentityPolicy for CookieIdentityPolicy {
fn from_request(&self, req: &mut ServiceRequest) -> Self::Future {
ok(self.0.load(req).map(
|CookieValue {
identity,
login_timestamp,
..
}| {
|CookieValue { identity, login_timestamp, .. }| {
if self.0.requires_oob_data() {
req.extensions_mut()
.insert(CookieIdentityExtention { login_timestamp });
@ -761,14 +752,12 @@ mod tests {
)
.secure(false)
.name(COOKIE_NAME))))
.service(web::resource("/").to(|id: Identity| {
async move {
let identity = id.identity();
if identity.is_none() {
id.remember(COOKIE_LOGIN.to_string())
}
web::Json(identity)
.service(web::resource("/").to(|id: Identity| async move {
let identity = id.identity();
if identity.is_none() {
id.remember(COOKIE_LOGIN.to_string())
}
web::Json(identity)
})),
)
.await
@ -822,12 +811,14 @@ mod tests {
assert_eq!(cookie.value(), identity);
}
#[allow(clippy::enum_variant_names)]
enum LoginTimestampCheck {
NoTimestamp,
NewTimestamp,
OldTimestamp(SystemTime),
}
#[allow(clippy::enum_variant_names)]
enum VisitTimeStampCheck {
NoTimestamp,
NewTimestamp,
@ -1108,12 +1099,12 @@ mod tests {
let mut srv = IdentityServiceMiddleware {
backend: Rc::new(Ident),
service: Rc::new(RefCell::new(into_service(|_: ServiceRequest| {
async move {
service: Rc::new(RefCell::new(into_service(
|_: ServiceRequest| async move {
actix_rt::time::delay_for(std::time::Duration::from_secs(100)).await;
Err::<ServiceResponse, _>(error::ErrorBadRequest("error"))
}
}))),
},
))),
};
let mut srv2 = srv.clone();

View File

@ -144,10 +144,10 @@ impl<T: Message + Default> Responder for ProtoBuf<T> {
self.0
.encode(&mut buf)
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
.and_then(|()| {
Ok(HttpResponse::Ok()
.map(|()| {
HttpResponse::Ok()
.content_type("application/protobuf")
.body(buf))
.body(buf)
}),
)
}

View File

@ -203,15 +203,13 @@ impl CookieSessionInner {
/// use actix_session::CookieSession;
/// use actix_web::{web, App, HttpResponse, HttpServer};
///
/// fn main() {
/// let app = App::new().wrap(
/// CookieSession::signed(&[0; 32])
/// .domain("www.rust-lang.org")
/// .name("actix_session")
/// .path("/")
/// .secure(true))
/// .service(web::resource("/").to(|| HttpResponse::Ok()));
/// }
/// let app = App::new().wrap(
/// CookieSession::signed(&[0; 32])
/// .domain("www.rust-lang.org")
/// .name("actix_session")
/// .path("/")
/// .secure(true))
/// .service(web::resource("/").to(|| HttpResponse::Ok()));
/// ```
pub struct CookieSession(Rc<CookieSessionInner>);
@ -256,7 +254,7 @@ impl CookieSession {
/// When true, prevents adding session cookies to responses until
/// the session contains data. Default is `false`.
///
///
/// Useful when trying to comply with laws that require consent for setting cookies.
pub fn lazy(mut self, value: bool) -> CookieSession {
Rc::get_mut(&mut self.0).unwrap().lazy = value;
@ -451,18 +449,13 @@ mod tests {
let _ = ses.set("counter", 100);
"counting"
}))
.service(web::resource("/").to(|_ses: Session| async move {
"test"
})),
.service(web::resource("/").to(|_ses: Session| async move { "test" })),
)
.await;
let request = test::TestRequest::get().to_request();
let response = app.call(request).await.unwrap();
assert!(response
.response()
.cookies()
.count() == 0);
assert!(response.response().cookies().count() == 0);
let request = test::TestRequest::with_uri("/count").to_request();
let response = app.call(request).await.unwrap();

View File

@ -276,7 +276,8 @@ mod tests {
let mut req = test::TestRequest::default().to_srv_request();
Session::set_session(
vec![("key".to_string(), serde_json::to_string("value").unwrap())].into_iter(),
vec![("key".to_string(), serde_json::to_string("value").unwrap())]
.into_iter(),
&mut req,
);
let session = Session::get_session(&mut *req.extensions_mut());