Skip to content

Commit

Permalink
fix a bug with multiple named pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiocode committed Aug 7, 2017
1 parent 626af65 commit 6660b4b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mux_test.go
Expand Up @@ -198,6 +198,40 @@ func TestMux(t *testing.T) {
res.Body.Close()
})

t.Run("router with multiple named pattern", func(t *testing.T) {
assert := assert.New(t)

mux := New()
mux.Get("/api/:type/:ID", func(w http.ResponseWriter, r *http.Request) {
params := Params(r)
w.WriteHeader(200)
w.Write([]byte(params[":type"] + params[":ID"]))
})

mux.Get("/api/:ID", func(w http.ResponseWriter, r *http.Request) {
params := Params(r)
w.WriteHeader(200)
w.Write([]byte(params[":ID"]))
})

ts := httptest.NewServer(mux)
defer ts.Close()

res, err := Request("GET", ts.URL+"/api/user/123", nil)
assert.Nil(err)
assert.Equal(200, res.StatusCode)
body, _ := ioutil.ReadAll(res.Body)
assert.Equal("user123", string(body))
res.Body.Close()

res, err = Request("GET", ts.URL+"/api/user/123", nil)
assert.Nil(err)
assert.Equal(200, res.StatusCode)
body, _ = ioutil.ReadAll(res.Body)
assert.Equal("user123", string(body))
res.Body.Close()
})

t.Run("router with double colon pattern", func(t *testing.T) {
assert := assert.New(t)

Expand Down
3 changes: 3 additions & 0 deletions tree.go
Expand Up @@ -480,6 +480,9 @@ func matchNode(parent *Node, segment, path string) (child *Node) {
if child.regex != nil && !child.regex.MatchString(segment) {
continue
}
if len(path) == 0 && len(child.optionChildren) == 0 && len(child.handlers) == 0 {
continue
}
return child
}
for _, child = range parent.varyChildren {
Expand Down

0 comments on commit 6660b4b

Please sign in to comment.