@@ -179,3 +179,44 @@ func TestNilApp(t *testing.T) {
179
179
t .Error ("wrong response body" , respBody )
180
180
}
181
181
}
182
+
183
+ func errorStatus (c * gin.Context ) {
184
+ c .String (500 , "an error happened" )
185
+ }
186
+
187
+ func TestStatusCodes (t * testing.T ) {
188
+ // Test that we are correctly able to collect status code.
189
+ // This behavior changed with this pull request: https://github.com/gin-gonic/gin/pull/1606
190
+ // In Gin v1.4.0 and below, we always recorded a 200 status, whereas with
191
+ // newer Gin versions we now correctly capture the status.
192
+ app := testApp (t )
193
+ router := gin .Default ()
194
+ router .Use (Middleware (app ))
195
+ router .GET ("/err" , errorStatus )
196
+
197
+ response := httptest .NewRecorder ()
198
+ req , err := http .NewRequest ("GET" , "/err" , nil )
199
+ if err != nil {
200
+ t .Fatal (err )
201
+ }
202
+ router .ServeHTTP (response , req )
203
+ if respBody := response .Body .String (); respBody != "an error happened" {
204
+ t .Error ("wrong response body" , respBody )
205
+ }
206
+ if response .Code != 500 {
207
+ t .Error ("wrong response code" , response .Code )
208
+ }
209
+ app .(internal.Expect ).ExpectTxnEvents (t , []internal.WantEvent {{
210
+ Intrinsics : map [string ]interface {}{
211
+ "name" : "WebTransaction/Go/" + pkg + ".errorStatus" ,
212
+ "nr.apdexPerfZone" : internal .MatchAnything ,
213
+ },
214
+ UserAttributes : map [string ]interface {}{},
215
+ AgentAttributes : map [string ]interface {}{
216
+ "httpResponseCode" : 500 ,
217
+ "request.method" : "GET" ,
218
+ "request.uri" : "/err" ,
219
+ "response.headers.contentType" : "text/plain; charset=utf-8" ,
220
+ },
221
+ }})
222
+ }
0 commit comments