Skip to content

Commit

Permalink
Added method Route.GetMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin authored and kisielk committed May 20, 2017
1 parent 1856953 commit b552615
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,7 @@ type routeTest struct {
path string // the expected path of the match
pathTemplate string // the expected path template to match
hostTemplate string // the expected host template to match
methods []string // the expected route methods
pathRegexp string // the expected path regexp
shouldMatch bool // whether the request is expected to match the route at all
shouldRedirect bool // whether the request should result in a redirect
Expand Down Expand Up @@ -660,6 661,7 @@ func TestMethods(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
methods: []string{"GET", "POST"},
shouldMatch: true,
},
{
Expand All @@ -669,6 671,7 @@ func TestMethods(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
methods: []string{"GET", "POST"},
shouldMatch: true,
},
{
Expand All @@ -678,13 681,25 @@ func TestMethods(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
methods: []string{"GET", "POST"},
shouldMatch: false,
},
{
title: "Route without methods",
route: new(Route),
request: newRequest("PUT", "http://localhost"),
vars: map[string]string{},
host: "",
path: "",
methods: []string{},
shouldMatch: true,
},
}

for _, test := range tests {
testRoute(t, test)
testTemplate(t, test)
testMethods(t, test)
}
}

Expand Down Expand Up @@ -1512,6 1527,14 @@ func testTemplate(t *testing.T, test routeTest) {
}
}

func testMethods(t *testing.T, test routeTest) {
route := test.route
methods, _ := route.GetMethods()
if strings.Join(methods, ",") != strings.Join(test.methods, ",") {
t.Errorf("(%v) GetMethods not equal: expected %v, got %v", test.title, test.methods, methods)
}
}

func testRegexp(t *testing.T, test routeTest) {
route := test.route
routePathRegexp, regexpErr := route.GetPathRegexp()
Expand Down
16 changes: 16 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 572,22 @@ func (r *Route) GetPathRegexp() (string, error) {
return r.regexp.path.regexp.String(), nil
}

// GetMethods returns the methods the route matches against
// This is useful for building simple REST API documentation and for instrumentation
// against third-party services.
// An empty list will be returned if route does not have methods.
func (r *Route) GetMethods() ([]string, error) {
if r.err != nil {
return nil, r.err
}
for _, m := range r.matchers {
if methods, ok := m.(methodMatcher); ok {
return []string(methods), nil
}
}
return nil, nil
}

// GetHostTemplate returns the template used to build the
// route match.
// This is useful for building simple REST API documentation and for instrumentation
Expand Down

0 comments on commit b552615

Please sign in to comment.