Skip to content

Commit

Permalink
expose Group's path
Browse files Browse the repository at this point in the history
  • Loading branch information
delaneyj committed Jun 21, 2023
1 parent 9b0fb2d commit 42a8800
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
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
14 changes: 11 additions & 3 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,27 @@ 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)
r, _ := http.NewRequest(method, fullUserPath+method, nil)
router.ServeHTTP(w, r)

if expect == "" {
Expand All @@ -137,6 +145,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")
}

0 comments on commit 42a8800

Please sign in to comment.