@@ -14,7 +14,7 @@ import (
1414 "github.com/stretchr/testify/assert"
1515)
1616
17- func TestGroup_withoutRouteWillNotExecuteMiddleware (t * testing.T ) {
17+ func TestGroup_withoutRouteWillExecuteMiddleware (t * testing.T ) {
1818 e := New ()
1919
2020 called := false
@@ -24,17 +24,18 @@ func TestGroup_withoutRouteWillNotExecuteMiddleware(t *testing.T) {
2424 return c .NoContent (http .StatusTeapot )
2525 }
2626 }
27- // even though group has middleware it will not be executed when there are no routes under that group
27+ // even though group has middleware it will be executed when there are no routes under that group
28+ // because implicit routes ("" and "/*") are created for the group
2829 _ = e .Group ("/group" , mw )
2930
3031 status , body := request (http .MethodGet , "/group/nope" , e )
31- assert .Equal (t , http .StatusNotFound , status )
32- assert .Equal (t , `{"message":"Not Found"}` + " \n " , body )
32+ assert .Equal (t , http .StatusTeapot , status )
33+ assert .Equal (t , " " , body )
3334
34- assert .False (t , called )
35+ assert .True (t , called )
3536}
3637
37- func TestGroup_withRoutesWillNotExecuteMiddlewareFor404 (t * testing.T ) {
38+ func TestGroup_withRoutesWillExecuteMiddlewareFor404 (t * testing.T ) {
3839 e := New ()
3940
4041 called := false
@@ -45,15 +46,17 @@ func TestGroup_withRoutesWillNotExecuteMiddlewareFor404(t *testing.T) {
4546 }
4647 }
4748 // even though group has middleware and routes when we have no match on some route the middlewares for that
48- // group will not be executed
49+ // group will be executed
4950 g := e .Group ("/group" , mw )
5051 g .GET ("/yes" , handlerFunc )
5152
53+ // route was `/group/yes` but we are requesting `/group/nope` which will result 404 by Router, but middleware will be
54+ // not reach the handler and return 418
5255 status , body := request (http .MethodGet , "/group/nope" , e )
53- assert .Equal (t , http .StatusNotFound , status )
54- assert .Equal (t , `{"message":"Not Found"}` + " \n " , body )
56+ assert .Equal (t , http .StatusTeapot , status )
57+ assert .Equal (t , " " , body )
5558
56- assert .False (t , called )
59+ assert .True (t , called )
5760}
5861
5962func TestGroup_multiLevelGroup (t * testing.T ) {
@@ -407,7 +410,9 @@ func TestGroup_Match(t *testing.T) {
407410}
408411
409412func TestGroup_MatchWithErrors (t * testing.T ) {
410- e := New ()
413+ e := NewWithConfig (Config {
414+ Router : NewRouter (RouterConfig {AllowOverwritingRoute : false }), // to trigger "duplicate route" error
415+ })
411416
412417 users := e .Group ("/users" )
413418 users .GET ("/activate" , func (c * Context ) error {
@@ -752,25 +757,25 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
752757 name : "ok, custom 404 handler is called with middleware" ,
753758 givenCustom404 : true ,
754759 whenURL : "/group/test3" ,
755- expectBody : "404 GET /group/*" ,
760+ expectBody : "404 (local) GET /group/*" ,
756761 expectCode : http .StatusNotFound ,
757762 expectMiddlewareCalled : true , // because RouteNotFound is added after middleware is added
758763 },
759764 {
760- name : "ok, default group 404 handler is not called with middleware" ,
765+ name : "ok, default group 404 handler is called with middleware" ,
761766 givenCustom404 : false ,
762767 whenURL : "/group/test3" ,
763- expectBody : "404 GET /*" ,
768+ expectBody : "404 (global) GET /group /*" ,
764769 expectCode : http .StatusNotFound ,
765- expectMiddlewareCalled : false , // because RouteNotFound is added before middleware is added
770+ expectMiddlewareCalled : true , // because RouteNotFound is added before middleware is added
766771 },
767772 {
768773 name : "ok, (no slash) default group 404 handler is called with middleware" ,
769774 givenCustom404 : false ,
770775 whenURL : "/group" ,
771- expectBody : "404 GET /* " ,
776+ expectBody : "404 (global) GET /group " ,
772777 expectCode : http .StatusNotFound ,
773- expectMiddlewareCalled : false , // because RouteNotFound is added before middleware is added
778+ expectMiddlewareCalled : true , // because RouteNotFound is added before middleware is added
774779 },
775780 }
776781 for _ , tc := range testCases {
@@ -779,13 +784,23 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
779784 okHandler := func (c * Context ) error {
780785 return c .String (http .StatusOK , c .Request ().Method + " " + c .Path ())
781786 }
782- notFoundHandler := func (c * Context ) error {
783- return c .String (http .StatusNotFound , "404 " + c .Request ().Method + " " + c .Path ())
787+ old404 := notFoundHandler
788+ defer func () { notFoundHandler = old404 }()
789+
790+ localNotFoundHandler := func (c * Context ) error {
791+ return c .String (http .StatusNotFound , "404 (local) " + c .Request ().Method + " " + c .Path ())
784792 }
785793
786- e := New ()
794+ e := NewWithConfig (Config {
795+ Router : NewRouter (RouterConfig {
796+ AllowOverwritingRoute : true ,
797+ NotFoundHandler : func (c * Context ) error {
798+ return c .String (http .StatusNotFound , "404 (global) " + c .Request ().Method + " " + c .Path ())
799+ },
800+ }),
801+ })
787802 e .GET ("/test1" , okHandler )
788- e .RouteNotFound ("/*" , notFoundHandler )
803+ e .RouteNotFound ("/*" , localNotFoundHandler )
789804
790805 g := e .Group ("/group" )
791806 g .GET ("/test1" , okHandler )
@@ -798,7 +813,7 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
798813 }
799814 })
800815 if tc .givenCustom404 {
801- g .RouteNotFound ("/*" , notFoundHandler )
816+ g .RouteNotFound ("/*" , localNotFoundHandler )
802817 }
803818
804819 req := httptest .NewRequest (http .MethodGet , tc .whenURL , nil )
0 commit comments