@@ -703,3 +703,77 @@ func TestJWTConfig_SuccessHandler(t *testing.T) {
703703 })
704704 }
705705}
706+
707+ func TestJWTConfig_NoErrorContinuesExecution (t * testing.T ) {
708+ var testCases = []struct {
709+ name string
710+ whenNoErrorContinuesExecution bool
711+ givenToken string
712+ expectStatus int
713+ expectBody string
714+ }{
715+ {
716+ name : "no error handler is called" ,
717+ whenNoErrorContinuesExecution : true ,
718+ givenToken : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" ,
719+ expectStatus : http .StatusTeapot ,
720+ expectBody : "" ,
721+ },
722+ {
723+ name : "NoErrorContinuesExecution is false and error handler is called for missing token" ,
724+ whenNoErrorContinuesExecution : false ,
725+ givenToken : "" ,
726+ // empty response with 200. This emulates previous behaviour when error handler swallowed the error
727+ expectStatus : http .StatusOK ,
728+ expectBody : "" ,
729+ },
730+ {
731+ name : "error handler is called for missing token" ,
732+ whenNoErrorContinuesExecution : true ,
733+ givenToken : "" ,
734+ expectStatus : http .StatusTeapot ,
735+ expectBody : "public-token" ,
736+ },
737+ {
738+ name : "error handler is called for invalid token" ,
739+ whenNoErrorContinuesExecution : true ,
740+ givenToken : "x.x.x" ,
741+ expectStatus : http .StatusUnauthorized ,
742+ expectBody : "{\" message\" :\" Unauthorized\" }\n " ,
743+ },
744+ }
745+
746+ for _ , tc := range testCases {
747+ t .Run (tc .name , func (t * testing.T ) {
748+ e := echo .New ()
749+
750+ e .GET ("/" , func (c echo.Context ) error {
751+ testValue , _ := c .Get ("test" ).(string )
752+ return c .String (http .StatusTeapot , testValue )
753+ })
754+
755+ e .Use (JWTWithConfig (JWTConfig {
756+ NoErrorContinuesExecution : tc .whenNoErrorContinuesExecution ,
757+ SigningKey : []byte ("secret" ),
758+ ErrorHandlerWithContext : func (err error , c echo.Context ) error {
759+ if err == ErrJWTMissing {
760+ c .Set ("test" , "public-token" )
761+ return nil
762+ }
763+ return echo .ErrUnauthorized
764+ },
765+ }))
766+
767+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
768+ if tc .givenToken != "" {
769+ req .Header .Set (echo .HeaderAuthorization , "bearer " + tc .givenToken )
770+ }
771+ res := httptest .NewRecorder ()
772+
773+ e .ServeHTTP (res , req )
774+
775+ assert .Equal (t , tc .expectStatus , res .Code )
776+ assert .Equal (t , tc .expectBody , res .Body .String ())
777+ })
778+ }
779+ }
0 commit comments