Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #609 and #636 #637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ func TestMuxRegexp2(t *testing.T) {
ts := httptest.NewServer(r)
defer ts.Close()

if _, body := testRequest(t, ts, "GET", "/foo-.json", nil); body != "" {
if resp, body := testRequest(t, ts, "GET", "/foo-.json", nil); resp.StatusCode != http.StatusNotFound {
t.Fatalf(body)
}
if _, body := testRequest(t, ts, "GET", "/foo-abc.json", nil); body != "abc" {
Expand Down
5 changes: 1 addition & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,11 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
} else {
continue
}
} else if ntyp == ntRegexp && p == 0 {
continue
}

if ntyp == ntRegexp && xn.rex != nil {
if !xn.rex.MatchString(xsearch[:p]) {
xn = nil
continue
}
} else if strings.IndexByte(xsearch[:p], '/') != -1 {
Expand Down Expand Up @@ -471,8 +470,6 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
xsearch = search
}

rctx.routeParams.Values = append(rctx.routeParams.Values, "")

default:
// catch-all nodes
rctx.routeParams.Values = append(rctx.routeParams.Values, search)
Expand Down
27 changes: 22 additions & 5 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

func TestTree(t *testing.T) {
hStub := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
misMatch1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
misMatch2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hIndex := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hFavicon := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hArticleList := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
Expand All @@ -35,6 +37,9 @@ func TestTree(t *testing.T) {
tr.InsertRoute(mGET, "/", hIndex)
tr.InsertRoute(mGET, "/favicon.ico", hFavicon)

tr.InsertRoute(mGET, "/{:[a-z]+}/", misMatch1)
tr.InsertRoute(mGET, "/{:[1-9]\\d*}/test", misMatch2)

tr.InsertRoute(mGET, "/pages/*", hStub)

tr.InsertRoute(mGET, "/article", hArticleList)
Expand Down Expand Up @@ -78,14 +83,19 @@ func TestTree(t *testing.T) {
tr.InsertRoute(mGET, "/hubs/{hubID}/users", hHubView3)

tests := []struct {
r string // input request path
h http.Handler // output matched handler
k []string // output param keys
v []string // output param values
r string // input request path
h http.Handler // output matched handler
k []string // output param keys
v []string // output param values
mismatch bool // if true, it means the match should fail
}{
{r: "/", h: hIndex, k: []string{}, v: []string{}},
{r: "/favicon.ico", h: hFavicon, k: []string{}, v: []string{}},

{r: "//", h: misMatch1, k: []string{}, v: []string{}, mismatch: true},
{r: "/123/test", h: misMatch2, k: []string{""}, v: []string{"123"}},
{r: "//test", h: misMatch2, k: []string{}, v: []string{}, mismatch: true},

{r: "/pages", h: nil, k: []string{}, v: []string{}},
{r: "/pages/", h: hStub, k: []string{"*"}, v: []string{""}},
{r: "/pages/yes", h: hStub, k: []string{"*"}, v: []string{"yes"}},
Expand Down Expand Up @@ -129,7 +139,14 @@ func TestTree(t *testing.T) {
for i, tt := range tests {
rctx := NewRouteContext()

_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
n, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
if tt.mismatch && n != nil {
t.Errorf("input [%d]: find '%s' should mismatch but get node(%+v)!", i, tt.r, n)
continue
}
if tt.mismatch {
continue
}

var handler http.Handler
if methodHandler, ok := handlers[mGET]; ok {
Expand Down