Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
helios741 committed Jul 13, 2021
1 parent a63dd1f commit 6c47f79
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
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

0 comments on commit 6c47f79

Please sign in to comment.