@@ -503,67 +503,33 @@ func Test_TokenFromContext_None(t *testing.T) {
503
503
}
504
504
505
505
func Test_TokenFromContext (t * testing.T ) {
506
- // Test that TokenFromContext returns the correct token
507
- t .Run ("fiber.Ctx" , func (t * testing.T ) {
508
- app := fiber .New ()
509
- app .Use (New (Config {
510
- KeyLookup : "header:Authorization" ,
511
- AuthScheme : "Basic" ,
512
- Validator : func (_ fiber.Ctx , key string ) (bool , error ) {
513
- if key == CorrectKey {
514
- return true , nil
515
- }
516
- return false , ErrMissingOrMalformedAPIKey
517
- },
518
- }))
519
- app .Get ("/" , func (c fiber.Ctx ) error {
520
- return c .SendString (TokenFromContext (c ))
521
- })
522
-
523
- req := httptest .NewRequest (fiber .MethodGet , "/" , nil )
524
- req .Header .Add ("Authorization" , "Basic " + CorrectKey )
525
- res , err := app .Test (req )
526
- require .NoError (t , err )
527
-
528
- body , err := io .ReadAll (res .Body )
529
- require .NoError (t , err )
530
- require .Equal (t , CorrectKey , string (body ))
506
+ app := fiber .New ()
507
+ // Wire up keyauth middleware to set TokenFromContext now
508
+ app .Use (New (Config {
509
+ KeyLookup : "header:Authorization" ,
510
+ AuthScheme : "Basic" ,
511
+ Validator : func (_ fiber.Ctx , key string ) (bool , error ) {
512
+ if key == CorrectKey {
513
+ return true , nil
514
+ }
515
+ return false , ErrMissingOrMalformedAPIKey
516
+ },
517
+ }))
518
+ // Define a test handler that checks TokenFromContext
519
+ app .Get ("/" , func (c fiber.Ctx ) error {
520
+ return c .SendString (TokenFromContext (c ))
531
521
})
532
522
533
- t .Run ("context.Context" , func (t * testing.T ) {
534
- app := fiber .New ()
535
- app .Use (New (Config {
536
- KeyLookup : "header:Authorization" ,
537
- AuthScheme : "Basic" ,
538
- Validator : func (_ fiber.Ctx , key string ) (bool , error ) {
539
- if key == CorrectKey {
540
- return true , nil
541
- }
542
- return false , ErrMissingOrMalformedAPIKey
543
- },
544
- }))
545
- // Verify that TokenFromContext works with context.Context
546
- app .Get ("/" , func (c fiber.Ctx ) error {
547
- ctx := c .Context ()
548
- token := TokenFromContext (ctx )
549
- return c .SendString (token )
550
- })
551
-
552
- req := httptest .NewRequest (fiber .MethodGet , "/" , nil )
553
- req .Header .Add ("Authorization" , "Basic " + CorrectKey )
554
- res , err := app .Test (req )
555
- require .NoError (t , err )
556
-
557
- body , err := io .ReadAll (res .Body )
558
- require .NoError (t , err )
559
- require .Equal (t , CorrectKey , string (body ))
560
- })
523
+ req := httptest .NewRequest (fiber .MethodGet , "/" , nil )
524
+ req .Header .Add ("Authorization" , "Basic " + CorrectKey )
525
+ // Send
526
+ res , err := app .Test (req )
527
+ require .NoError (t , err )
561
528
562
- t .Run ("invalid context type" , func (t * testing.T ) {
563
- require .Panics (t , func () {
564
- _ = TokenFromContext ("invalid" )
565
- })
566
- })
529
+ // Read the response body into a string
530
+ body , err := io .ReadAll (res .Body )
531
+ require .NoError (t , err )
532
+ require .Equal (t , CorrectKey , string (body ))
567
533
}
568
534
569
535
func Test_AuthSchemeToken (t * testing.T ) {
0 commit comments