Skip to content

Commit bd0610a

Browse files
purple4reinaGitHub Enterprise
authored and
GitHub Enterprise
committed
Merge pull request #585 from go-agent/gin-test
A test to cover gin-gonic/gin#1606.
2 parents 128337c + 1c378ba commit bd0610a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

_integrations/nrgin/v1/nrgin_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,44 @@ func TestNilApp(t *testing.T) {
179179
t.Error("wrong response body", respBody)
180180
}
181181
}
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

Comments
 (0)