Skip to content

Commit 3f09087

Browse files
authored
add a new function RouteRegister which supports *gin.RouterGroup… (gin-contrib#20)
first parameter
1 parent 021a0ad commit 3f09087

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

pprof.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ func getPrefix(prefixOptions ...string) string {
2424
// the provided gin.Engine. prefixOptions is a optional. If not prefixOptions,
2525
// the default path prefix is used, otherwise first prefixOptions will be path prefix.
2626
func Register(r *gin.Engine, prefixOptions ...string) {
27+
RouteRegister(&(r.RouterGroup), prefixOptions...)
28+
}
29+
30+
// RouteRegister the standard HandlerFuncs from the net/http/pprof package with
31+
// the provided gin.GrouterGroup. prefixOptions is a optional. If not prefixOptions,
32+
// the default path prefix is used, otherwise first prefixOptions will be path prefix.
33+
func RouteRegister(rg *gin.RouterGroup, prefixOptions ...string) {
2734
prefix := getPrefix(prefixOptions...)
2835

29-
prefixRouter := r.Group(prefix)
36+
prefixRouter := rg.Group(prefix)
3037
{
3138
prefixRouter.GET("/", pprofHandler(pprof.Index))
3239
prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))

pprof_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package pprof
22

3-
import "testing"
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/gin-gonic/gin"
9+
)
410

511
func Test_getPrefix(t *testing.T) {
612
tests := []struct {
@@ -18,3 +24,43 @@ func Test_getPrefix(t *testing.T) {
1824
}
1925
}
2026
}
27+
28+
func TestRegisterAndRouteRegister(t *testing.T) {
29+
bearerToken := "Bearer token"
30+
gin.SetMode(gin.ReleaseMode)
31+
r := gin.New()
32+
Register(r)
33+
adminGroup := r.Group("/admin", func(c *gin.Context) {
34+
if c.Request.Header.Get("Authorization") != bearerToken {
35+
c.AbortWithStatus(http.StatusForbidden)
36+
return
37+
}
38+
c.Next()
39+
})
40+
RouteRegister(adminGroup, "pprof")
41+
42+
req, _ := http.NewRequest(http.MethodGet, "/debug/pprof/", nil)
43+
rw := httptest.NewRecorder()
44+
r.ServeHTTP(rw, req)
45+
46+
if expected, got := http.StatusOK, rw.Code; expected != got {
47+
t.Errorf("expected: %d, got: %d", expected, got)
48+
}
49+
50+
req, _ = http.NewRequest(http.MethodGet, "/admin/pprof/", nil)
51+
rw = httptest.NewRecorder()
52+
r.ServeHTTP(rw, req)
53+
54+
if expected, got := http.StatusForbidden, rw.Code; expected != got {
55+
t.Errorf("expected: %d, got: %d", expected, got)
56+
}
57+
58+
req, _ = http.NewRequest(http.MethodGet, "/admin/pprof/", nil)
59+
req.Header.Set("Authorization", bearerToken)
60+
rw = httptest.NewRecorder()
61+
r.ServeHTTP(rw, req)
62+
63+
if expected, got := http.StatusOK, rw.Code; expected != got {
64+
t.Errorf("expected: %d, got: %d", expected, got)
65+
}
66+
}

0 commit comments

Comments
 (0)