Skip to content

Commit

Permalink
feat(#5): remove routes
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Apr 2, 2016
1 parent 71b7414 commit 318642e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
19 changes: 11 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,17 @@ func doMatch(routes []*Route, path string) (url.Values, *Route) {

// Remove matches and removes the matched route from the router stack.
func (r *Router) Remove(method, path string) bool {
// TODO
return true
}

// RemoveRoute removes the given route from the router stack.
func (r *Router) RemoveRoute(route *Route) bool {
// TODO
return true
routes := r.Routes[method]
if routes == nil || len(routes) == 0 {
return false
}
for i, route := range routes {
if _, ok := route.Match(path); ok {
r.Routes[method] = append(routes[:i], routes[i+1:]...)
return true
}
}
return false
}

// HandleHTTP matches r.URL.Path against its routing table
Expand Down
27 changes: 27 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ import (
"testing"
)

func TestRouterRemoveRoute(t *testing.T) {
p := New()
p.Get("/foo")
p.All("/bar")

st.Expect(t, len(p.Routes["GET"]), 1)
st.Expect(t, len(p.Routes["*"]), 1)

st.Expect(t, routeExists(p, "*", "/bar"), true)
st.Expect(t, routeExists(p, "GET", "/foo"), true)

st.Expect(t, p.Remove("GET", "/foo"), true)
st.Expect(t, len(p.Routes["GET"]), 0)
st.Expect(t, routeExists(p, "GET", "/foo"), false)

st.Expect(t, p.Remove("*", "/bar"), true)
st.Expect(t, len(p.Routes["*"]), 0)
st.Expect(t, routeExists(p, "*", "/bar"), false)

st.Expect(t, p.Remove("*", "/baz"), false)
}

func TestRoutingHit(t *testing.T) {
p := New()

Expand Down Expand Up @@ -184,3 +206,8 @@ func newRequest(method, urlStr string, body io.Reader) *http.Request {
}
return req
}

func routeExists(r *Router, method, path string) bool {
route, _ := r.FindRoute(method, path)
return route != nil
}

0 comments on commit 318642e

Please sign in to comment.