diff --git a/CHANGELOG.md b/CHANGELOG.md index f0cbc4f..f8b3312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## [1.0.21](https://github.com/uptrace/bunrouter/compare/v1.0.20...v1.0.21) (2023-06-21) + ## [1.0.20](https://github.com/uptrace/bunrouter/compare/v1.0.19...v1.0.20) (2023-02-12) diff --git a/group.go b/group.go index 2f82d00..d0e9b9a 100644 --- a/group.go +++ b/group.go @@ -79,6 +79,11 @@ func (g *Group) Handle(meth string, path string, handler HandlerFunc) { } } +// Expose path read-only. +func (g *Group) Path() string { + return g.path +} + // Syntactic sugar for Handle("GET", path, handler) func (g *Group) GET(path string, handler HandlerFunc) { g.Handle("GET", path, handler) diff --git a/group_test.go b/group_test.go index 31d1db5..6c81f23 100644 --- a/group_test.go +++ b/group_test.go @@ -1,6 +1,7 @@ package bunrouter import ( + "fmt" "net/http" "net/http/httptest" "testing" @@ -104,19 +105,28 @@ func testGroupMethods(t *testing.T) { } } router := New() + // Testing with a sub-group of a group as that will test everything at once - g := router.NewGroup("/base").NewGroup("/user") + const ( + basePath = "/base" + userPath = "/user" + fullUserPath = basePath + userPath + ) + g := router.NewGroup(basePath).NewGroup(userPath) g.GET("/:param", makeHandler("GET")) g.POST("/:param", makeHandler("POST")) g.PATCH("/:param", makeHandler("PATCH")) g.PUT("/:param", makeHandler("PUT")) g.DELETE("/:param", makeHandler("DELETE")) + require.Equal(t, g.Path(), fullUserPath) + testMethod := func(method, expect string) { result = "" w := httptest.NewRecorder() - r, _ := http.NewRequest(method, "/base/user/"+method, nil) + url := fmt.Sprintf("%s/%s", fullUserPath, method) + r, _ := http.NewRequest(method, url, nil) router.ServeHTTP(w, r) if expect == "" { @@ -137,6 +147,6 @@ func testGroupMethods(t *testing.T) { testMethod("DELETE", "DELETE") testMethod("HEAD", "") - router.HEAD("/base/user/:param", makeHandler("HEAD")) + router.HEAD(fullUserPath+"/:param", makeHandler("HEAD")) testMethod("HEAD", "HEAD") }