mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 18:37:41 +02:00
Update dependencies (Tokio 1.0) (#144)
This commit is contained in:
@ -54,7 +54,7 @@ impl<C: 'static + Challenge> ResponseError for AuthenticationError<C> {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::build(self.status_code)
|
||||
// TODO: Get rid of the `.clone()`
|
||||
.set(WwwAuthenticate(self.challenge.clone()))
|
||||
.insert_header(WwwAuthenticate(self.challenge.clone()))
|
||||
.finish()
|
||||
}
|
||||
|
||||
|
@ -91,8 +91,8 @@ impl<S: Scheme> Header for Authorization<S> {
|
||||
impl<S: Scheme> IntoHeaderValue for Authorization<S> {
|
||||
type Error = <S as IntoHeaderValue>::Error;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
self.0.try_into()
|
||||
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
|
||||
self.0.try_into_value()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ impl fmt::Display for Basic {
|
||||
impl IntoHeaderValue for Basic {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
let mut credentials = BytesMut::with_capacity(
|
||||
self.user_id.len()
|
||||
+ 1 // ':'
|
||||
@ -187,7 +187,7 @@ mod tests {
|
||||
password: Some("open sesame".into()),
|
||||
};
|
||||
|
||||
let result = basic.try_into();
|
||||
let result = basic.try_into_value();
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(
|
||||
result.unwrap(),
|
||||
|
@ -76,7 +76,7 @@ impl fmt::Display for Bearer {
|
||||
impl IntoHeaderValue for Bearer {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
let mut buffer = BytesMut::with_capacity(7 + self.token.len());
|
||||
buffer.put(&b"Bearer "[..]);
|
||||
buffer.extend_from_slice(self.token.as_bytes());
|
||||
@ -128,7 +128,7 @@ mod tests {
|
||||
fn test_into_header_value() {
|
||||
let bearer = Bearer::new("mF_9.B5f-4.1JqM");
|
||||
|
||||
let result = bearer.try_into();
|
||||
let result = bearer.try_into_value();
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(
|
||||
result.unwrap(),
|
||||
|
@ -25,7 +25,7 @@ use crate::utils;
|
||||
/// let challenge = Basic::with_realm("Restricted area");
|
||||
///
|
||||
/// HttpResponse::Unauthorized()
|
||||
/// .set(WwwAuthenticate(challenge))
|
||||
/// .insert_header(WwwAuthenticate(challenge))
|
||||
/// .finish()
|
||||
/// }
|
||||
/// ```
|
||||
@ -106,7 +106,7 @@ impl fmt::Display for Basic {
|
||||
impl IntoHeaderValue for Basic {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
HeaderValue::from_maybe_shared(self.to_bytes())
|
||||
}
|
||||
}
|
||||
@ -120,7 +120,7 @@ mod tests {
|
||||
fn test_plain_into_header_value() {
|
||||
let challenge = Basic { realm: None };
|
||||
|
||||
let value = challenge.try_into();
|
||||
let value = challenge.try_into_value();
|
||||
assert!(value.is_ok());
|
||||
let value = value.unwrap();
|
||||
assert_eq!(value, "Basic");
|
||||
@ -132,7 +132,7 @@ mod tests {
|
||||
realm: Some("Restricted area".into()),
|
||||
};
|
||||
|
||||
let value = challenge.try_into();
|
||||
let value = challenge.try_into_value();
|
||||
assert!(value.is_ok());
|
||||
let value = value.unwrap();
|
||||
assert_eq!(value, "Basic realm=\"Restricted area\"");
|
||||
|
@ -31,7 +31,7 @@ use crate::utils;
|
||||
/// .finish();
|
||||
///
|
||||
/// HttpResponse::Unauthorized()
|
||||
/// .set(WwwAuthenticate(challenge))
|
||||
/// .insert_header(WwwAuthenticate(challenge))
|
||||
/// .finish()
|
||||
/// }
|
||||
/// ```
|
||||
@ -133,7 +133,7 @@ impl fmt::Display for Bearer {
|
||||
impl IntoHeaderValue for Bearer {
|
||||
type Error = InvalidHeaderValue;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
HeaderValue::from_maybe_shared(self.to_bytes())
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ impl<C: Challenge> Header for WwwAuthenticate<C> {
|
||||
impl<C: Challenge> IntoHeaderValue for WwwAuthenticate<C> {
|
||||
type Error = <C as IntoHeaderValue>::Error;
|
||||
|
||||
fn try_into(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
self.0.try_into()
|
||||
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
|
||||
self.0.try_into_value()
|
||||
}
|
||||
}
|
||||
|
@ -118,16 +118,14 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B, T, F, O> Transform<S> for HttpAuthentication<T, F>
|
||||
impl<S, B, T, F, O> Transform<S, ServiceRequest> for HttpAuthentication<T, F>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>
|
||||
+ 'static,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
F: Fn(ServiceRequest, T) -> O + 'static,
|
||||
O: Future<Output = Result<ServiceRequest, Error>> + 'static,
|
||||
T: AuthExtractor + 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Transform = AuthenticationMiddleware<S, F, T>;
|
||||
@ -153,25 +151,23 @@ where
|
||||
_extractor: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<S, B, F, T, O> Service for AuthenticationMiddleware<S, F, T>
|
||||
impl<S, B, F, T, O> Service<ServiceRequest> for AuthenticationMiddleware<S, F, T>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>
|
||||
+ 'static,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
F: Fn(ServiceRequest, T) -> O + 'static,
|
||||
O: Future<Output = Result<ServiceRequest, Error>> + 'static,
|
||||
T: AuthExtractor + 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = S::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<ServiceResponse<B>, Error>>;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.borrow_mut().poll_ready(ctx)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
let process_fn = Arc::clone(&self.process_fn);
|
||||
|
||||
let service = Rc::clone(&self.service);
|
||||
@ -251,15 +247,14 @@ mod tests {
|
||||
use actix_service::{into_service, Service};
|
||||
use actix_web::error;
|
||||
use actix_web::test::TestRequest;
|
||||
use futures_util::join;
|
||||
|
||||
/// This is a test for https://github.com/actix/actix-extras/issues/10
|
||||
#[actix_rt::test]
|
||||
async fn test_middleware_panic() {
|
||||
let mut middleware = AuthenticationMiddleware {
|
||||
let middleware = AuthenticationMiddleware {
|
||||
service: Rc::new(RefCell::new(into_service(
|
||||
|_: ServiceRequest| async move {
|
||||
actix_rt::time::delay_for(std::time::Duration::from_secs(1)).await;
|
||||
actix_rt::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Err::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
},
|
||||
))),
|
||||
@ -267,22 +262,24 @@ mod tests {
|
||||
_extractor: PhantomData,
|
||||
};
|
||||
|
||||
let req = TestRequest::with_header("Authorization", "Bearer 1").to_srv_request();
|
||||
let req = TestRequest::get()
|
||||
.append_header(("Authorization", "Bearer 1"))
|
||||
.to_srv_request();
|
||||
|
||||
let f = middleware.call(req);
|
||||
let f = middleware.call(req).await;
|
||||
|
||||
let res = futures_util::future::lazy(|cx| middleware.poll_ready(cx));
|
||||
let _res = futures_util::future::lazy(|cx| middleware.poll_ready(cx)).await;
|
||||
|
||||
assert!(join!(f, res).0.is_err());
|
||||
assert!(f.is_err());
|
||||
}
|
||||
|
||||
/// This is a test for https://github.com/actix/actix-extras/issues/10
|
||||
#[actix_rt::test]
|
||||
async fn test_middleware_panic_several_orders() {
|
||||
let mut middleware = AuthenticationMiddleware {
|
||||
let middleware = AuthenticationMiddleware {
|
||||
service: Rc::new(RefCell::new(into_service(
|
||||
|_: ServiceRequest| async move {
|
||||
actix_rt::time::delay_for(std::time::Duration::from_secs(1)).await;
|
||||
actix_rt::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Err::<ServiceResponse, _>(error::ErrorBadRequest("error"))
|
||||
},
|
||||
))),
|
||||
@ -290,24 +287,28 @@ mod tests {
|
||||
_extractor: PhantomData,
|
||||
};
|
||||
|
||||
let req = TestRequest::with_header("Authorization", "Bearer 1").to_srv_request();
|
||||
let req = TestRequest::get()
|
||||
.append_header(("Authorization", "Bearer 1"))
|
||||
.to_srv_request();
|
||||
|
||||
let f1 = middleware.call(req);
|
||||
let f1 = middleware.call(req).await;
|
||||
|
||||
let req = TestRequest::with_header("Authorization", "Bearer 1").to_srv_request();
|
||||
let req = TestRequest::get()
|
||||
.append_header(("Authorization", "Bearer 1"))
|
||||
.to_srv_request();
|
||||
|
||||
let f2 = middleware.call(req);
|
||||
let f2 = middleware.call(req).await;
|
||||
|
||||
let req = TestRequest::with_header("Authorization", "Bearer 1").to_srv_request();
|
||||
let req = TestRequest::get()
|
||||
.append_header(("Authorization", "Bearer 1"))
|
||||
.to_srv_request();
|
||||
|
||||
let f3 = middleware.call(req);
|
||||
let f3 = middleware.call(req).await;
|
||||
|
||||
let res = futures_util::future::lazy(|cx| middleware.poll_ready(cx));
|
||||
let _res = futures_util::future::lazy(|cx| middleware.poll_ready(cx)).await;
|
||||
|
||||
let result = join!(f1, f2, f3, res);
|
||||
|
||||
assert!(result.0.is_err());
|
||||
assert!(result.1.is_err());
|
||||
assert!(result.2.is_err());
|
||||
assert!(f1.is_err());
|
||||
assert!(f2.is_err());
|
||||
assert!(f3.is_err());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user