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

View File

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

View File

@ -203,15 +203,13 @@ impl CookieSessionInner {
/// use actix_session::CookieSession; /// use actix_session::CookieSession;
/// use actix_web::{web, App, HttpResponse, HttpServer}; /// use actix_web::{web, App, HttpResponse, HttpServer};
/// ///
/// fn main() { /// let app = App::new().wrap(
/// let app = App::new().wrap( /// CookieSession::signed(&[0; 32])
/// CookieSession::signed(&[0; 32]) /// .domain("www.rust-lang.org")
/// .domain("www.rust-lang.org") /// .name("actix_session")
/// .name("actix_session") /// .path("/")
/// .path("/") /// .secure(true))
/// .secure(true)) /// .service(web::resource("/").to(|| HttpResponse::Ok()));
/// .service(web::resource("/").to(|| HttpResponse::Ok()));
/// }
/// ``` /// ```
pub struct CookieSession(Rc<CookieSessionInner>); pub struct CookieSession(Rc<CookieSessionInner>);
@ -451,18 +449,13 @@ mod tests {
let _ = ses.set("counter", 100); let _ = ses.set("counter", 100);
"counting" "counting"
})) }))
.service(web::resource("/").to(|_ses: Session| async move { .service(web::resource("/").to(|_ses: Session| async move { "test" })),
"test"
})),
) )
.await; .await;
let request = test::TestRequest::get().to_request(); let request = test::TestRequest::get().to_request();
let response = app.call(request).await.unwrap(); let response = app.call(request).await.unwrap();
assert!(response assert!(response.response().cookies().count() == 0);
.response()
.cookies()
.count() == 0);
let request = test::TestRequest::with_uri("/count").to_request(); let request = test::TestRequest::with_uri("/count").to_request();
let response = app.call(request).await.unwrap(); let response = app.call(request).await.unwrap();

View File

@ -276,7 +276,8 @@ mod tests {
let mut req = test::TestRequest::default().to_srv_request(); let mut req = test::TestRequest::default().to_srv_request();
Session::set_session( 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, &mut req,
); );
let session = Session::get_session(&mut *req.extensions_mut()); let session = Session::get_session(&mut *req.extensions_mut());