Skip to content

Commit

Permalink
🐞 fix: fix route matching issue, support path with trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Nov 21, 2024
1 parent d647887 commit 3abc61a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion znet/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (u utils) TreeFind(t *Tree, path string) (*Engine, handlerFn, []handlerFn,
}
}

if len(nodes) == 0 {
if len(nodes) == 0 || strings.HasSuffix(path, "/") {
res := strings.Split(path, "/")
p := ""
if len(res) == 1 {
Expand Down
16 changes: 16 additions & 0 deletions znet/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ func TestWeb(t *testing.T) {
})
tt.Equal(200, w.Code)
tt.Equal(expected, w.Body.String())

r.GET("/web-get/*", func(c *Context) string {
return "web-get:" + c.GetParam("*")
})

w = request(r, "GET", "/web-get/", nil)
tt.Equal(200, w.Code)
tt.Equal("web-get:", w.Body.String())

w = request(r, "GET", "/web-get/1", nil)
tt.Equal(200, w.Code)
tt.Equal("web-get:1", w.Body.String())

w = request(r, "GET", "/web-get/a", nil)
tt.Equal(200, w.Code)
tt.Equal("web-get:a", w.Body.String())
}

func TestMoreMethod(t *testing.T) {
Expand Down

0 comments on commit 3abc61a

Please sign in to comment.