about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-28 14:04:28 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-28 14:04:28 -0700
commitf97475a5404b5693a47cbbee19adf81c650b13f2 (patch)
tree48967c97b532b6f04df24cf25219f836d387e6f4
parentUse `knownPaths` to return the list of paths in GetRouteList. (diff)
downloadpath-router-f97475a5404b5693a47cbbee19adf81c650b13f2.tar.gz
Add a few more methods for future introspection to the Router.
The HasPath method check if a path is know by the router and return a
boolean.

The RemovePath method is useful to remove dynamically a path from the
router.

The GetAllRoutesByMethods returns a list of routes that match a given
method.

I'm not sure I'll keep all those methods available since there's no
immutability on the routes, and I don't want the user to mess with
them.  We will see later.
-rw-r--r--router.go41
-rw-r--r--router_test.go64
2 files changed, 101 insertions, 4 deletions
diff --git a/router.go b/router.go
index d5bcfd0..c40713d 100644
--- a/router.go
+++ b/router.go
@@ -99,8 +99,41 @@ func (self *Router) GetRouteList() []string {
 	return routes
 }
 
-// func (*dispatcher) AddRoutes() {
-// }
+func (self *Router) HasPath(path string) bool {
+	if self.knownPaths[path] != nil {
+		return true
+	}
+	return false
+}
+
+func (self *Router) RemovePath(path string) error {
+	p := self.HasPath(path)
+	if p == false {
+		return errors.New("foo")
+	}
+	delete(self.knownPaths, path)
+
+	newRoutes := []*Route{}
 
-// func (self *dispatcher) UriFor() {
-// }
+	for _, p := range self.routes {
+		if p.Path != path {
+			newRoutes = append(newRoutes, p)
+		}
+	}
+	self.routes = newRoutes
+	return nil
+}
+
+func (self *Router) GetAllRoutes() []*Route {
+	return self.routes
+}
+
+func (self *Router) GetAllRoutesByMethods (method string) []*Route {
+	routes := []*Route{}
+	for _, r := range self.routes {
+		if r.Method == method {
+			routes = append(routes, r)
+		}
+	}
+	return routes
+}
diff --git a/router_test.go b/router_test.go
index a9826ba..3b94699 100644
--- a/router_test.go
+++ b/router_test.go
@@ -36,3 +36,67 @@ func TestGetRouteList(t *testing.T) {
 		t.Fatal()
 	}
 }
+
+func TestGetHasPath(t *testing.T) {
+	r := BuildRouter()
+	r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute})
+	r.AddRoute(&Route{Method: "GET", Path: "/bar", Code: testRoute})
+	r.AddRoute(&Route{Method: "GET", Path: "/baz", Code: testRoute})
+
+	if p := r.HasPath("/foo"); p == false {
+		t.Fatal()
+	}
+
+	if p := r.HasPath("/foo/bar/baz"); p == true {
+		t.Fatal()
+	}
+}
+
+func TestRemovePath(t *testing.T) {
+	r := BuildRouter()
+	r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute})
+
+	err := r.RemovePath("/foo")
+	if err != nil {
+		t.Fatal()
+	}
+
+	err = r.RemovePath("/foo")
+	if err == nil {
+		t.Fatal()
+	}
+}
+
+func TestGetAllRoutes(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})
+
+	routes := r.GetAllRoutes()
+	if len(routes) != 3 {
+		t.Fatal()
+	}
+}
+
+func TestGetAllRoutesByMethod(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})
+
+	routes := r.GetAllRoutesByMethods("GET")
+	if len(routes) != 2 {
+		t.Fatal()
+	}
+
+	routes = r.GetAllRoutesByMethods("PUT")
+	if len(routes) != 1 {
+		t.Fatal()
+	}
+
+	routes = r.GetAllRoutesByMethods("foobar")
+	if len(routes) != 0 {
+		t.Fatal()
+	}
+}