@@ -303,6 +303,71 @@ func TestGroup_TRACE(t *testing.T) {
303
303
assert .Equal (t , `OK` , body )
304
304
}
305
305
306
+ func TestGroup_RouteNotFound (t * testing.T ) {
307
+ var testCases = []struct {
308
+ name string
309
+ whenURL string
310
+ expectRoute interface {}
311
+ expectCode int
312
+ }{
313
+ {
314
+ name : "404, route to static not found handler /group/a/c/xx" ,
315
+ whenURL : "/group/a/c/xx" ,
316
+ expectRoute : "GET /group/a/c/xx" ,
317
+ expectCode : http .StatusNotFound ,
318
+ },
319
+ {
320
+ name : "404, route to path param not found handler /group/a/:file" ,
321
+ whenURL : "/group/a/echo.exe" ,
322
+ expectRoute : "GET /group/a/:file" ,
323
+ expectCode : http .StatusNotFound ,
324
+ },
325
+ {
326
+ name : "404, route to any not found handler /group/*" ,
327
+ whenURL : "/group/b/echo.exe" ,
328
+ expectRoute : "GET /group/*" ,
329
+ expectCode : http .StatusNotFound ,
330
+ },
331
+ {
332
+ name : "200, route /group/a/c/df to /group/a/c/df" ,
333
+ whenURL : "/group/a/c/df" ,
334
+ expectRoute : "GET /group/a/c/df" ,
335
+ expectCode : http .StatusOK ,
336
+ },
337
+ }
338
+
339
+ for _ , tc := range testCases {
340
+ t .Run (tc .name , func (t * testing.T ) {
341
+ e := New ()
342
+ g := e .Group ("/group" )
343
+
344
+ okHandler := func (c Context ) error {
345
+ return c .String (http .StatusOK , c .Request ().Method + " " + c .Path ())
346
+ }
347
+ notFoundHandler := func (c Context ) error {
348
+ return c .String (http .StatusNotFound , c .Request ().Method + " " + c .Path ())
349
+ }
350
+
351
+ g .GET ("/" , okHandler )
352
+ g .GET ("/a/c/df" , okHandler )
353
+ g .GET ("/a/b*" , okHandler )
354
+ g .PUT ("/*" , okHandler )
355
+
356
+ g .RouteNotFound ("/a/c/xx" , notFoundHandler ) // static
357
+ g .RouteNotFound ("/a/:file" , notFoundHandler ) // param
358
+ g .RouteNotFound ("/*" , notFoundHandler ) // any
359
+
360
+ req := httptest .NewRequest (http .MethodGet , tc .whenURL , nil )
361
+ rec := httptest .NewRecorder ()
362
+
363
+ e .ServeHTTP (rec , req )
364
+
365
+ assert .Equal (t , tc .expectCode , rec .Code )
366
+ assert .Equal (t , tc .expectRoute , rec .Body .String ())
367
+ })
368
+ }
369
+ }
370
+
306
371
func TestGroup_Any (t * testing.T ) {
307
372
e := New ()
308
373
0 commit comments