-
Notifications
You must be signed in to change notification settings - Fork 59
/
pprof_test.go
66 lines (56 loc) · 1.63 KB
/
pprof_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package pprof
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func Test_getPrefix(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{"default value", nil, "/debug/pprof"},
{"test user input value", []string{"test/pprof"}, "test/pprof"},
{"test user input value", []string{"test/pprof", "pprof"}, "test/pprof"},
}
for _, tt := range tests {
if got := getPrefix(tt.args...); got != tt.want {
t.Errorf("%q. getPrefix() = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestRegisterAndRouteRegister(t *testing.T) {
bearerToken := "Bearer token"
gin.SetMode(gin.ReleaseMode)
r := gin.New()
Register(r)
adminGroup := r.Group("/admin", func(c *gin.Context) {
if c.Request.Header.Get("Authorization") != bearerToken {
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
})
RouteRegister(adminGroup, "pprof")
req, _ := http.NewRequest(http.MethodGet, "/debug/pprof/", nil)
rw := httptest.NewRecorder()
r.ServeHTTP(rw, req)
if expected, got := http.StatusOK, rw.Code; expected != got {
t.Errorf("expected: %d, got: %d", expected, got)
}
req, _ = http.NewRequest(http.MethodGet, "/admin/pprof/", nil)
rw = httptest.NewRecorder()
r.ServeHTTP(rw, req)
if expected, got := http.StatusForbidden, rw.Code; expected != got {
t.Errorf("expected: %d, got: %d", expected, got)
}
req, _ = http.NewRequest(http.MethodGet, "/admin/pprof/", nil)
req.Header.Set("Authorization", bearerToken)
rw = httptest.NewRecorder()
r.ServeHTTP(rw, req)
if expected, got := http.StatusOK, rw.Code; expected != got {
t.Errorf("expected: %d, got: %d", expected, got)
}
}