@@ -1013,6 +1013,150 @@ func TestEchoMethodNotAllowed(t *testing.T) {
10131013 assert .Equal (t , "OPTIONS, GET" , rec .Header ().Get (HeaderAllow ))
10141014}
10151015
1016+ func TestEcho_OnAddRoute (t * testing.T ) {
1017+ type rr struct {
1018+ host string
1019+ path string
1020+ }
1021+ exampleRoute := Route {
1022+ Method : http .MethodGet ,
1023+ Path : "/api/files/:id" ,
1024+ Handler : notFoundHandler ,
1025+ Middlewares : nil ,
1026+ Name : "x" ,
1027+ }
1028+
1029+ var testCases = []struct {
1030+ name string
1031+ whenHost string
1032+ whenRoute Routable
1033+ whenError error
1034+ expectLen int
1035+ expectAdded []rr
1036+ expectError string
1037+ }{
1038+ {
1039+ name : "ok, for default host" ,
1040+ whenHost : "" ,
1041+ whenRoute : exampleRoute ,
1042+ whenError : nil ,
1043+ expectAdded : []rr {
1044+ {host : "" , path : "/static" },
1045+ {host : "" , path : "/api/files/:id" },
1046+ },
1047+ expectError : "" ,
1048+ expectLen : 2 ,
1049+ },
1050+ {
1051+ name : "ok, for specific host" ,
1052+ whenHost : "test.com" ,
1053+ whenRoute : exampleRoute ,
1054+ whenError : nil ,
1055+ expectAdded : []rr {
1056+ {host : "" , path : "/static" },
1057+ {host : "test.com" , path : "/api/files/:id" },
1058+ },
1059+ expectError : "" ,
1060+ expectLen : 1 ,
1061+ },
1062+ {
1063+ name : "nok, error is returned" ,
1064+ whenHost : "test.com" ,
1065+ whenRoute : exampleRoute ,
1066+ whenError : errors .New ("nope" ),
1067+ expectAdded : []rr {
1068+ {host : "" , path : "/static" },
1069+ },
1070+ expectError : "nope" ,
1071+ expectLen : 0 ,
1072+ },
1073+ }
1074+ for _ , tc := range testCases {
1075+ t .Run (tc .name , func (t * testing.T ) {
1076+
1077+ e := New ()
1078+
1079+ added := make ([]rr , 0 )
1080+ cnt := 0
1081+ e .OnAddRoute = func (host string , route Routable ) error {
1082+ if cnt > 0 && tc .whenError != nil { // we want to GET /static to succeed for nok tests
1083+ return tc .whenError
1084+ }
1085+ cnt ++
1086+ added = append (added , rr {
1087+ host : host ,
1088+ path : route .ToRoute ().Path ,
1089+ })
1090+ return nil
1091+ }
1092+
1093+ e .GET ("/static" , notFoundHandler )
1094+
1095+ var err error
1096+ if tc .whenHost != "" {
1097+ _ , err = e .Host (tc .whenHost ).AddRoute (tc .whenRoute )
1098+ } else {
1099+ _ , err = e .AddRoute (tc .whenRoute )
1100+ }
1101+
1102+ if tc .expectError != "" {
1103+ assert .EqualError (t , err , tc .expectError )
1104+ } else {
1105+ assert .NoError (t , err )
1106+ }
1107+
1108+ r , _ := e .RouterFor (tc .whenHost )
1109+ assert .Len (t , r .Routes (), tc .expectLen )
1110+ assert .Equal (t , tc .expectAdded , added )
1111+ })
1112+ }
1113+ }
1114+
1115+ func TestEcho_RouterFor (t * testing.T ) {
1116+ var testCases = []struct {
1117+ name string
1118+ whenHost string
1119+ expectLen int
1120+ expectOk bool
1121+ }{
1122+ {
1123+ name : "ok, default host" ,
1124+ whenHost : "" ,
1125+ expectLen : 2 ,
1126+ expectOk : true ,
1127+ },
1128+ {
1129+ name : "ok, specific host with routes" ,
1130+ whenHost : "test.com" ,
1131+ expectLen : 1 ,
1132+ expectOk : true ,
1133+ },
1134+ {
1135+ name : "ok, non-existent host" ,
1136+ whenHost : "oups.com" ,
1137+ expectLen : 0 ,
1138+ expectOk : false ,
1139+ },
1140+ }
1141+ for _ , tc := range testCases {
1142+ t .Run (tc .name , func (t * testing.T ) {
1143+ e := New ()
1144+
1145+ e .GET ("/1" , notFoundHandler )
1146+ e .GET ("/2" , notFoundHandler )
1147+ e .Host ("test.com" ).GET ("/3" , notFoundHandler )
1148+
1149+ r , ok := e .RouterFor (tc .whenHost )
1150+ assert .Equal (t , tc .expectOk , ok )
1151+ if tc .expectLen > 0 {
1152+ assert .Len (t , r .Routes (), tc .expectLen )
1153+ } else {
1154+ assert .Nil (t , r )
1155+ }
1156+ })
1157+ }
1158+ }
1159+
10161160func TestEchoContext (t * testing.T ) {
10171161 e := New ()
10181162 c := e .AcquireContext ()
0 commit comments