@@ -166,7 +166,7 @@ macro_rules! predicate_required {
166
166
} ;
167
167
168
168
from_fn(
169
- |auth_session: $crate:: AuthSession <_>,
169
+ move |auth_session: $crate:: AuthSession <_>,
170
170
OriginalUri ( original_uri) : OriginalUri ,
171
171
req,
172
172
next: Next | async move {
@@ -382,6 +382,49 @@ mod tests {
382
382
assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
383
383
}
384
384
385
+ #[ tokio:: test]
386
+ async fn test_login_required_with_configured_login_url ( ) {
387
+ // let tes_login_url: &'static str = Box::leak("/login".to_string().into_boxed_str());
388
+ let test_login_url: & ' static str = "/login" ;
389
+ let app = Router :: new ( )
390
+ . route ( "/" , axum:: routing:: get ( || async { } ) )
391
+ . route_layer ( login_required ! ( Backend , login_url = test_login_url) )
392
+ . route (
393
+ "/login" ,
394
+ axum:: routing:: get ( |mut auth_session : AuthSession < Backend > | async move {
395
+ auth_session. login ( & User ) . await . unwrap ( ) ;
396
+ } ) ,
397
+ )
398
+ . layer ( auth_layer ! ( ) ) ;
399
+
400
+ let req = Request :: builder ( ) . uri ( "/" ) . body ( Body :: empty ( ) ) . unwrap ( ) ;
401
+ let res = app. clone ( ) . oneshot ( req) . await . unwrap ( ) ;
402
+
403
+ assert_eq ! ( res. status( ) , StatusCode :: TEMPORARY_REDIRECT ) ;
404
+ assert_eq ! (
405
+ res. headers( )
406
+ . get( header:: LOCATION )
407
+ . and_then( |h| h. to_str( ) . ok( ) ) ,
408
+ Some ( "/login?next=%2F" )
409
+ ) ;
410
+
411
+ let req = Request :: builder ( )
412
+ . uri ( "/login" )
413
+ . body ( Body :: empty ( ) )
414
+ . unwrap ( ) ;
415
+ let res = app. clone ( ) . oneshot ( req) . await . unwrap ( ) ;
416
+ let session_cookie =
417
+ get_session_cookie ( & res) . expect ( "Response should have a valid session cookie" ) ;
418
+
419
+ let req = Request :: builder ( )
420
+ . uri ( "/" )
421
+ . header ( header:: COOKIE , session_cookie)
422
+ . body ( Body :: empty ( ) )
423
+ . unwrap ( ) ;
424
+ let res = app. oneshot ( req) . await . unwrap ( ) ;
425
+ assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
426
+ }
427
+
385
428
#[ tokio:: test]
386
429
async fn test_login_required_with_login_url_and_redirect_field ( ) {
387
430
let app = Router :: new ( )
0 commit comments