@@ -21,6 +21,7 @@ func TestMiddleware(t *testing.T) {
21
21
tests := map [string ]struct {
22
22
handlerID string
23
23
config middleware.Config
24
+ route string
24
25
req func () * http.Request
25
26
mock func (m * mmetrics.Recorder )
26
27
handler func () gin.HandlerFunc
@@ -86,6 +87,37 @@ func TestMiddleware(t *testing.T) {
86
87
expRespCode : 202 ,
87
88
expRespBody : `{"test":"one"}` ,
88
89
},
90
+
91
+ "A default HTTP middleware should set template route as route label" : {
92
+ route : "/test/:id" ,
93
+ req : func () * http.Request {
94
+ return httptest .NewRequest (http .MethodPost , "/test/12" , nil )
95
+ },
96
+ mock : func (m * mmetrics.Recorder ) {
97
+ expHTTPReqProps := metrics.HTTPReqProperties {
98
+ ID : "/test/:id" ,
99
+ Service : "" ,
100
+ Method : "POST" ,
101
+ Code : "202" ,
102
+ }
103
+ m .On ("ObserveHTTPRequestDuration" , mock .Anything , expHTTPReqProps , mock .Anything ).Once ()
104
+ m .On ("ObserveHTTPResponseSize" , mock .Anything , expHTTPReqProps , int64 (14 )).Once ()
105
+
106
+ expHTTPProps := metrics.HTTPProperties {
107
+ ID : "/test/:id" ,
108
+ Service : "" ,
109
+ }
110
+ m .On ("AddInflightRequests" , mock .Anything , expHTTPProps , 1 ).Once ()
111
+ m .On ("AddInflightRequests" , mock .Anything , expHTTPProps , - 1 ).Once ()
112
+ },
113
+ handler : func () gin.HandlerFunc {
114
+ return func (c * gin.Context ) {
115
+ c .JSON (202 , map [string ]string {"test" : "one" })
116
+ }
117
+ },
118
+ expRespCode : 202 ,
119
+ expRespBody : `{"test":"one"}` ,
120
+ },
89
121
}
90
122
91
123
for name , test := range tests {
@@ -101,7 +133,12 @@ func TestMiddleware(t *testing.T) {
101
133
mdlw := middleware .New (middleware.Config {Recorder : mr })
102
134
engine := gin .New ()
103
135
req := test .req ()
104
- engine .Handle (req .Method , req .URL .Path ,
136
+
137
+ relativePath := req .URL .Path
138
+ if test .route != "" {
139
+ relativePath = test .route
140
+ }
141
+ engine .Handle (req .Method , relativePath ,
105
142
ginmiddleware .Handler (test .handlerID , mdlw ),
106
143
test .handler ())
107
144
0 commit comments