@@ -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,7 +24,29 @@ 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
29+ _ = e .Group ("/group" , mw )
30+
31+ status , body := request (http .MethodGet , "/group/nope" , e )
32+ assert .Equal (t , http .StatusTeapot , status )
33+ assert .Equal (t , "" , body )
34+
35+ assert .True (t , called )
36+ }
37+
38+ func TestGroup_withoutRouteWillNotExecuteMiddleware (t * testing.T ) {
39+ e := NewWithConfig (Config {NoGroupAutoRegister404Routes : true })
40+
41+ called := false
42+ mw := func (next HandlerFunc ) HandlerFunc {
43+ return func (c * Context ) error {
44+ called = true
45+ return c .NoContent (http .StatusTeapot )
46+ }
47+ }
48+ // even though group has middleware it will be executed when there are no routes under that group
49+ // because implicit routes ("" and "/*") are created for the group
2850 _ = e .Group ("/group" , mw )
2951
3052 status , body := request (http .MethodGet , "/group/nope" , e )
@@ -34,7 +56,7 @@ func TestGroup_withoutRouteWillNotExecuteMiddleware(t *testing.T) {
3456 assert .False (t , called )
3557}
3658
37- func TestGroup_withRoutesWillNotExecuteMiddlewareFor404 (t * testing.T ) {
59+ func TestGroup_withRoutesWillExecuteMiddlewareFor404 (t * testing.T ) {
3860 e := New ()
3961
4062 called := false
@@ -45,15 +67,17 @@ func TestGroup_withRoutesWillNotExecuteMiddlewareFor404(t *testing.T) {
4567 }
4668 }
4769 // 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
70+ // group will be executed
4971 g := e .Group ("/group" , mw )
5072 g .GET ("/yes" , handlerFunc )
5173
74+ // route was `/group/yes` but we are requesting `/group/nope` which will result 404 by Router, but middleware will be
75+ // not reach the handler and return 418
5276 status , body := request (http .MethodGet , "/group/nope" , e )
53- assert .Equal (t , http .StatusNotFound , status )
54- assert .Equal (t , `{"message":"Not Found"}` + " \n " , body )
77+ assert .Equal (t , http .StatusTeapot , status )
78+ assert .Equal (t , " " , body )
5579
56- assert .False (t , called )
80+ assert .True (t , called )
5781}
5882
5983func TestGroup_multiLevelGroup (t * testing.T ) {
@@ -425,7 +449,9 @@ func TestGroup_Match(t *testing.T) {
425449}
426450
427451func TestGroup_MatchWithErrors (t * testing.T ) {
428- e := New ()
452+ e := NewWithConfig (Config {
453+ Router : NewRouter (RouterConfig {AllowOverwritingRoute : false }), // to trigger "duplicate route" error
454+ })
429455
430456 users := e .Group ("/users" )
431457 users .GET ("/activate" , func (c * Context ) error {
@@ -770,25 +796,25 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
770796 name : "ok, custom 404 handler is called with middleware" ,
771797 givenCustom404 : true ,
772798 whenURL : "/group/test3" ,
773- expectBody : "404 GET /group/*" ,
799+ expectBody : "404 (local) GET /group/*" ,
774800 expectCode : http .StatusNotFound ,
775801 expectMiddlewareCalled : true , // because RouteNotFound is added after middleware is added
776802 },
777803 {
778- name : "ok, default group 404 handler is not called with middleware" ,
804+ name : "ok, default group 404 handler is called with middleware" ,
779805 givenCustom404 : false ,
780806 whenURL : "/group/test3" ,
781- expectBody : "404 GET /*" ,
807+ expectBody : "404 (global) GET /group /*" ,
782808 expectCode : http .StatusNotFound ,
783- expectMiddlewareCalled : false , // because RouteNotFound is added before middleware is added
809+ expectMiddlewareCalled : true , // because RouteNotFound is added before middleware is added
784810 },
785811 {
786812 name : "ok, (no slash) default group 404 handler is called with middleware" ,
787813 givenCustom404 : false ,
788814 whenURL : "/group" ,
789- expectBody : "404 GET /* " ,
815+ expectBody : "404 (global) GET /group " ,
790816 expectCode : http .StatusNotFound ,
791- expectMiddlewareCalled : false , // because RouteNotFound is added before middleware is added
817+ expectMiddlewareCalled : true , // because RouteNotFound is added before middleware is added
792818 },
793819 }
794820 for _ , tc := range testCases {
@@ -797,13 +823,23 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
797823 okHandler := func (c * Context ) error {
798824 return c .String (http .StatusOK , c .Request ().Method + " " + c .Path ())
799825 }
800- notFoundHandler := func (c * Context ) error {
801- return c .String (http .StatusNotFound , "404 " + c .Request ().Method + " " + c .Path ())
826+ old404 := notFoundHandler
827+ defer func () { notFoundHandler = old404 }()
828+
829+ localNotFoundHandler := func (c * Context ) error {
830+ return c .String (http .StatusNotFound , "404 (local) " + c .Request ().Method + " " + c .Path ())
802831 }
803832
804- e := New ()
833+ e := NewWithConfig (Config {
834+ Router : NewRouter (RouterConfig {
835+ AllowOverwritingRoute : true ,
836+ NotFoundHandler : func (c * Context ) error {
837+ return c .String (http .StatusNotFound , "404 (global) " + c .Request ().Method + " " + c .Path ())
838+ },
839+ }),
840+ })
805841 e .GET ("/test1" , okHandler )
806- e .RouteNotFound ("/*" , notFoundHandler )
842+ e .RouteNotFound ("/*" , localNotFoundHandler )
807843
808844 g := e .Group ("/group" )
809845 g .GET ("/test1" , okHandler )
@@ -816,7 +852,7 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
816852 }
817853 })
818854 if tc .givenCustom404 {
819- g .RouteNotFound ("/*" , notFoundHandler )
855+ g .RouteNotFound ("/*" , localNotFoundHandler )
820856 }
821857
822858 req := httptest .NewRequest (http .MethodGet , tc .whenURL , nil )
0 commit comments