Skip to content

Commit b552615

Browse files
bgaifullinkisielk
authored andcommitted
Added method Route.GetMethods
1 parent 1856953 commit b552615

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

mux_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type routeTest struct {
3535
path string // the expected path of the match
3636
pathTemplate string // the expected path template to match
3737
hostTemplate string // the expected host template to match
38+
methods []string // the expected route methods
3839
pathRegexp string // the expected path regexp
3940
shouldMatch bool // whether the request is expected to match the route at all
4041
shouldRedirect bool // whether the request should result in a redirect
@@ -660,6 +661,7 @@ func TestMethods(t *testing.T) {
660661
vars: map[string]string{},
661662
host: "",
662663
path: "",
664+
methods: []string{"GET", "POST"},
663665
shouldMatch: true,
664666
},
665667
{
@@ -669,6 +671,7 @@ func TestMethods(t *testing.T) {
669671
vars: map[string]string{},
670672
host: "",
671673
path: "",
674+
methods: []string{"GET", "POST"},
672675
shouldMatch: true,
673676
},
674677
{
@@ -678,13 +681,25 @@ func TestMethods(t *testing.T) {
678681
vars: map[string]string{},
679682
host: "",
680683
path: "",
684+
methods: []string{"GET", "POST"},
681685
shouldMatch: false,
682686
},
687+
{
688+
title: "Route without methods",
689+
route: new(Route),
690+
request: newRequest("PUT", "http://localhost"),
691+
vars: map[string]string{},
692+
host: "",
693+
path: "",
694+
methods: []string{},
695+
shouldMatch: true,
696+
},
683697
}
684698

685699
for _, test := range tests {
686700
testRoute(t, test)
687701
testTemplate(t, test)
702+
testMethods(t, test)
688703
}
689704
}
690705

@@ -1512,6 +1527,14 @@ func testTemplate(t *testing.T, test routeTest) {
15121527
}
15131528
}
15141529

1530+
func testMethods(t *testing.T, test routeTest) {
1531+
route := test.route
1532+
methods, _ := route.GetMethods()
1533+
if strings.Join(methods, ",") != strings.Join(test.methods, ",") {
1534+
t.Errorf("(%v) GetMethods not equal: expected %v, got %v", test.title, test.methods, methods)
1535+
}
1536+
}
1537+
15151538
func testRegexp(t *testing.T, test routeTest) {
15161539
route := test.route
15171540
routePathRegexp, regexpErr := route.GetPathRegexp()

route.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,22 @@ func (r *Route) GetPathRegexp() (string, error) {
572572
return r.regexp.path.regexp.String(), nil
573573
}
574574

575+
// GetMethods returns the methods the route matches against
576+
// This is useful for building simple REST API documentation and for instrumentation
577+
// against third-party services.
578+
// An empty list will be returned if route does not have methods.
579+
func (r *Route) GetMethods() ([]string, error) {
580+
if r.err != nil {
581+
return nil, r.err
582+
}
583+
for _, m := range r.matchers {
584+
if methods, ok := m.(methodMatcher); ok {
585+
return []string(methods), nil
586+
}
587+
}
588+
return nil, nil
589+
}
590+
575591
// GetHostTemplate returns the template used to build the
576592
// route match.
577593
// This is useful for building simple REST API documentation and for instrumentation

0 commit comments

Comments
 (0)