about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--router.go11
-rw-r--r--router_test.go29
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()
+	}
+}