Skip to content

Commit

Permalink
correcting infinte loop issue when backtracking over url param nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
husobee committed May 9, 2017
1 parent f9752ac commit 038b65b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (r *Router) find(req *http.Request) (prefix string, h http.HandlerFunc) {

// Search order static > param > match-any
for {

if search == "" {
if cn.resource != nil {
// Found route, check if method is applicable
Expand Down Expand Up @@ -317,7 +318,7 @@ func (r *Router) find(req *http.Request) (prefix string, h http.HandlerFunc) {
// last ditch effort to match on wildcard (issue #8)
var tmpsearch = search
for {
if cn != nil && cn.parent != nil {
if cn != nil && cn.parent != nil && cn.prefix != ":" {
tmpsearch = cn.prefix + tmpsearch
cn = cn.parent
if cn.prefix == "/" {
Expand Down
20 changes: 20 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,23 @@ func TestRouter_SearchSmallerThanPrefix(t *testing.T) {
assert.Equal(t, w.Body.String(), "custom not found")

}

func TestRouter_Issue64(t *testing.T) {
r := NewRouter()

r.Get("/foo", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "foo") })
r.Get("/:lang/foo", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "foo") })

// OK
normalRequest, _ := http.NewRequest("GET", "/hello/world", nil)

_, h := r.find(normalRequest)

w := httptest.NewRecorder()
if assert.NotNil(t, h) {
h(w, normalRequest)
}

assert.Equal(t, w.Body.String(), "custom not found")

}

0 comments on commit 038b65b

Please sign in to comment.