@@ -716,6 +716,67 @@ func TestRouterParam(t *testing.T) {
716716 }
717717}
718718
719+ func TestRouter_addAndMatchAllSupportedMethods (t * testing.T ) {
720+ var testCases = []struct {
721+ name string
722+ givenNoAddRoute bool
723+ whenMethod string
724+ expectPath string
725+ expectError string
726+ }{
727+ {name : "ok, CONNECT" , whenMethod : http .MethodConnect },
728+ {name : "ok, DELETE" , whenMethod : http .MethodDelete },
729+ {name : "ok, GET" , whenMethod : http .MethodGet },
730+ {name : "ok, HEAD" , whenMethod : http .MethodHead },
731+ {name : "ok, OPTIONS" , whenMethod : http .MethodOptions },
732+ {name : "ok, PATCH" , whenMethod : http .MethodPatch },
733+ {name : "ok, POST" , whenMethod : http .MethodPost },
734+ {name : "ok, PROPFIND" , whenMethod : PROPFIND },
735+ {name : "ok, PUT" , whenMethod : http .MethodPut },
736+ {name : "ok, TRACE" , whenMethod : http .MethodTrace },
737+ {name : "ok, REPORT" , whenMethod : REPORT },
738+ {name : "ok, NON_TRADITIONAL_METHOD" , whenMethod : "NON_TRADITIONAL_METHOD" },
739+ {
740+ name : "ok, NOT_EXISTING_METHOD" ,
741+ whenMethod : "NOT_EXISTING_METHOD" ,
742+ givenNoAddRoute : true ,
743+ expectPath : "/*" ,
744+ expectError : "code=405, message=Method Not Allowed" ,
745+ },
746+ }
747+
748+ for _ , tc := range testCases {
749+ t .Run (tc .name , func (t * testing.T ) {
750+ e := New ()
751+
752+ e .GET ("/*" , handlerFunc )
753+
754+ if ! tc .givenNoAddRoute {
755+ e .Add (tc .whenMethod , "/my/*" , handlerFunc )
756+ }
757+
758+ req := httptest .NewRequest (tc .whenMethod , "/my/some-url" , nil )
759+ rec := httptest .NewRecorder ()
760+ c := e .NewContext (req , rec ).(* context )
761+
762+ e .router .Find (tc .whenMethod , "/my/some-url" , c )
763+ err := c .handler (c )
764+
765+ if tc .expectError != "" {
766+ assert .EqualError (t , err , tc .expectError )
767+ } else {
768+ assert .NoError (t , err )
769+ }
770+
771+ expectPath := "/my/*"
772+ if tc .expectPath != "" {
773+ expectPath = tc .expectPath
774+ }
775+ assert .Equal (t , expectPath , c .Path ())
776+ })
777+ }
778+ }
779+
719780func TestMethodNotAllowedAndNotFound (t * testing.T ) {
720781 e := New ()
721782 r := e .router
@@ -2634,6 +2695,25 @@ func TestRouterHandleMethodOptions(t *testing.T) {
26342695 }
26352696}
26362697
2698+ func TestRouterAllowHeaderForAnyOtherMethodType (t * testing.T ) {
2699+ e := New ()
2700+ r := e .router
2701+
2702+ r .Add (http .MethodGet , "/users" , handlerFunc )
2703+ r .Add ("COPY" , "/users" , handlerFunc )
2704+ r .Add ("LOCK" , "/users" , handlerFunc )
2705+
2706+ req := httptest .NewRequest ("TEST" , "/users" , nil )
2707+ rec := httptest .NewRecorder ()
2708+ c := e .NewContext (req , rec ).(* context )
2709+
2710+ r .Find ("TEST" , "/users" , c )
2711+ err := c .handler (c )
2712+
2713+ assert .EqualError (t , err , "code=405, message=Method Not Allowed" )
2714+ assert .ElementsMatch (t , []string {"COPY" , "GET" , "LOCK" , "OPTIONS" }, strings .Split (c .Response ().Header ().Get (HeaderAllow ), ", " ))
2715+ }
2716+
26372717func benchmarkRouterRoutes (b * testing.B , routes []* Route , routesToFind []* Route ) {
26382718 e := New ()
26392719 r := e .router
0 commit comments