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

expose Group's path #91

Open
wants to merge 4 commits 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: 2 additions & 0 deletions 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)


Expand Down
5 changes: 5 additions & 0 deletions group.go
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions group_test.go
@@ -1,6 +1,7 @@
package bunrouter

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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")
}