From 8eae5e3b8c61eb2ed31c86ae672c2395b0ed02a0 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 28 Apr 2013 14:50:46 -0700 Subject: Add a method to get the HTTP methods supported for a path. --- router.go | 11 +++++++++++ router_test.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/router.go b/router.go index c40713d..4c9dfab 100644 --- a/router.go +++ b/router.go @@ -137,3 +137,14 @@ func (self *Router) GetAllRoutesByMethods (method string) []*Route { } return routes } + +func (self *Router) GetMethodsForPath(path string) []string { + p := self.knownPaths[path] + m := make([]string, len(p)) + i := 0 + for k, _ := range p { + m[i] = k + i = i + 1 + } + return m +} diff --git a/router_test.go b/router_test.go index 3b94699..de13225 100644 --- a/router_test.go +++ b/router_test.go @@ -4,18 +4,18 @@ import ( "testing" ) -func testRouter (req *Request) (Response, error) { +func testRouter(req *Request) (Response, error) { resp := Response{} return resp, nil } func testBasic(t *testing.T) { d := BuildRouter() - err := d.AddRoute(&Route{Method:"GET", Path:"/", Code: testRouter}) + err := d.AddRoute(&Route{Method: "GET", Path: "/", Code: testRouter}) if err != nil { t.Fatal() } - err = d.AddRoute(&Route{Method: "GET", Path:"/", Code: testRouter}) + err = d.AddRoute(&Route{Method: "GET", Path: "/", Code: testRouter}) if err == nil { t.Fatal() } @@ -100,3 +100,26 @@ func TestGetAllRoutesByMethod(t *testing.T) { t.Fatal() } } + +func TestGetMethodsForPath(t *testing.T) { + r := BuildRouter() + r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) + r.AddRoute(&Route{Method: "GET", Path: "/foo/bar", Code: testRoute}) + r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) + r.AddRoute(&Route{Method: "POST", Path: "/foo", Code: testRoute}) + + m := r.GetMethodsForPath("/foo") + if len(m) != 3 { + t.Fatal() + } + + m = r.GetMethodsForPath("/foo/bar") + if len(m) != 1 { + t.Fatal() + } + + m = r.GetMethodsForPath("/foo/bar/baz") + if len(m) != 0 { + t.Fatal() + } +} -- cgit 1.4.1