- Module actix_web_httpauth::
source · [−]
Module actix_web_httpauth::middleware
source · [−]Expand description
Expand description
HTTP Authentication middleware.
Structs
Middleware for checking HTTP authentication.
diff --git a/actix_web_httpauth/middleware/index.html b/actix_web_httpauth/middleware/index.html
index 458c81cae..e15b033c2 100644
--- a/actix_web_httpauth/middleware/index.html
+++ b/actix_web_httpauth/middleware/index.html
@@ -4,7 +4,7 @@
HTTP Authentication middleware. Middleware for checking HTTP authentication.
- Module actix_web_httpauth::
source · [−]Expand description
Expand description
Structs
//! HTTP Authentication middleware.
use std::{
@@ -602,10 +657,13 @@
#[cfg(test)]
mod tests {
use super::*;
+ use crate::extractors::basic::BasicAuth;
use crate::extractors::bearer::BearerAuth;
use actix_service::{into_service, Service};
+ use actix_web::error::ErrorForbidden;
+ use actix_web::http::StatusCode;
use actix_web::test::TestRequest;
- use actix_web::{error, HttpResponse};
+ use actix_web::{error, web, App, HttpResponse};
/// This is a test for https://github.com/actix/actix-extras/issues/10
#[actix_web::test]
@@ -716,6 +774,58 @@
assert!(f.is_ok());
}
+
+ #[actix_rt::test]
+ async fn test_middleware_works_with_app() {
+ async fn validator(
+ _req: ServiceRequest,
+ _credentials: BasicAuth,
+ ) -> Result<ServiceRequest, actix_web::Error> {
+ Err(ErrorForbidden("You are not welcome!"))
+ }
+ let middleware = HttpAuthentication::basic(validator);
+
+ let srv = actix_web::test::init_service(
+ App::new()
+ .wrap(middleware)
+ .route("/", web::get().to(|| web::HttpResponse::Ok())),
+ )
+ .await;
+
+ let req = actix_web::test::TestRequest::with_uri("/")
+ .append_header(("Authorization", "Basic DontCare"))
+ .to_request();
+
+ let resp = srv.call(req).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
+ }
+
+ #[actix_rt::test]
+ async fn test_middleware_works_with_scope() {
+ async fn validator(
+ _req: ServiceRequest,
+ _credentials: BasicAuth,
+ ) -> Result<ServiceRequest, actix_web::Error> {
+ Err(ErrorForbidden("You are not welcome!"))
+ }
+ let middleware = actix_web::middleware::Compat::new(HttpAuthentication::basic(validator));
+
+ let srv = actix_web::test::init_service(
+ App::new().service(
+ web::scope("/")
+ .wrap(middleware)
+ .route("/", web::get().to(|| web::HttpResponse::Ok())),
+ ),
+ )
+ .await;
+
+ let req = actix_web::test::TestRequest::with_uri("/")
+ .append_header(("Authorization", "Basic DontCare"))
+ .to_request();
+
+ let resp = srv.call(req).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
+ }
}