From 1f62eadff49a15c08d7a28c8760054cd9af3b829 Mon Sep 17 00:00:00 2001 From: robjtede Date: Tue, 1 Mar 2022 04:22:48 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20=20@=20ce92f?= =?UTF-8?q?0036f277ea33435a66c5dbceb014b1ba781=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actix_web_httpauth/middleware/index.html | 2 +- src/actix_web_httpauth/middleware.rs.html | 112 +++++++++++++++++++++- 2 files changed, 112 insertions(+), 2 deletions(-) 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 @@
Expand description

HTTP Authentication middleware.

+

Module actix_web_httpauth::middleware

source · []
Expand description

HTTP Authentication middleware.

Structs

Middleware for checking HTTP authentication.

diff --git a/src/actix_web_httpauth/middleware.rs.html b/src/actix_web_httpauth/middleware.rs.html index 4405b6391..f0df9303c 100644 --- a/src/actix_web_httpauth/middleware.rs.html +++ b/src/actix_web_httpauth/middleware.rs.html @@ -360,6 +360,61 @@ 355 356 357 +358 +359 +360 +361 +362 +363 +364 +365 +366 +367 +368 +369 +370 +371 +372 +373 +374 +375 +376 +377 +378 +379 +380 +381 +382 +383 +384 +385 +386 +387 +388 +389 +390 +391 +392 +393 +394 +395 +396 +397 +398 +399 +400 +401 +402 +403 +404 +405 +406 +407 +408 +409 +410 +411 +412
//! 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);
+    }
 }