Skip to content

Commit

Permalink
Added GetAllRoutes to Routes interface
Browse files Browse the repository at this point in the history
Closes #227
  • Loading branch information
José Miguel Molina Arboledas committed Apr 24, 2014
1 parent f8f9fcb commit e1b8dea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ type Routes interface {
URLFor(name string, params ...interface{}) string
// MethodsFor returns an array of methods available for the path
MethodsFor(path string) []string
// GetAllRoutes returns an array with all the routes in the router.
GetAllRoutes() []RouteInfo
}

// URLFor returns the url for the given route name.
Expand Down Expand Up @@ -280,6 +282,22 @@ func (r *router) URLFor(name string, params ...interface{}) string {
return route.URLWith(args)
}

// RouteInfo contains information about a route
type RouteInfo struct {
Pattern string
Method string
}

func (r *router) GetAllRoutes() []RouteInfo {
var ri = make([]RouteInfo, len(r.routes))

for i, route := range r.routes {
ri[i] = RouteInfo{route.pattern, route.method}
}

return ri
}

func hasMethod(methods []string, method string) bool {
for _, v := range methods {
if v == method {
Expand Down
16 changes: 16 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,19 @@ func Test_URLFor(t *testing.T) {
context.MapTo(router, (*Routes)(nil))
router.Handle(recorder, req, context)
}

func Test_GetAllRoutes(t *testing.T) {
router := NewRouter()

patterns := []string{"/foo", "/fee", "/fii"}
methods := []string{"GET", "POST", "DELETE"}

router.Get("/foo", func() {})
router.Post("/fee", func() {})
router.Delete("/fii", func() {})

for i, r := range router.GetAllRoutes() {
expect(t, r.Pattern, patterns[i])
expect(t, r.Method, methods[i])
}
}

0 comments on commit e1b8dea

Please sign in to comment.