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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nrgin
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal/integrationsupport"
"github.com/newrelic/go-agent/v3/newrelic"
)
var (
pkg = "github.com/newrelic/go-agent/v3/integrations/nrgin"
)
func hello(c *gin.Context) {
c.Writer.WriteString("hello response")
}
func TestBasicRoute(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/hello", hello)
txnName := "GET " + pkg + ".hello"
if useFullPathVersion(gin.Version) {
txnName = "GET /hello"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "hello response" {
t.Error("wrong response body", respBody)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
})
}
func TestRouterGroup(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
group := router.Group("/group")
group.GET("/hello", hello)
txnName := "GET " + pkg + ".hello"
if useFullPathVersion(gin.Version) {
txnName = "GET /group/hello"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/group/hello", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "hello response" {
t.Error("wrong response body", respBody)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
})
}
func TestAnonymousHandler(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/anon", func(c *gin.Context) {
c.Writer.WriteString("anonymous function handler")
})
txnName := "GET " + pkg + ".TestAnonymousHandler.func1"
if useFullPathVersion(gin.Version) {
txnName = "GET /anon"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/anon", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "anonymous function handler" {
t.Error("wrong response body", respBody)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
})
}
func multipleWriteHeader(c *gin.Context) {
// Unlike http.ResponseWriter, gin.ResponseWriter does not immediately
// write the first WriteHeader. Instead, it gets buffered until the
// first Write call.
c.Writer.WriteHeader(200)
c.Writer.WriteHeader(500)
c.Writer.WriteString("multipleWriteHeader")
}
func TestMultipleWriteHeader(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/header", multipleWriteHeader)
txnName := "GET " + pkg + ".multipleWriteHeader"
if useFullPathVersion(gin.Version) {
txnName = "GET /header"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/header", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "multipleWriteHeader" {
t.Error("wrong response body", respBody)
}
if response.Code != 500 {
t.Error("wrong response code", response.Code)
}
// Error metrics test the 500 response code capture.
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
NumErrors: 1,
})
}
func accessTransactionGinContext(c *gin.Context) {
txn := Transaction(c)
txn.NoticeError(errors.New("problem"))
c.Writer.WriteString("accessTransactionGinContext")
}
func TestContextTransaction(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/txn", accessTransactionGinContext)
txnName := "GET " + pkg + ".accessTransactionGinContext"
if useFullPathVersion(gin.Version) {
txnName = "GET /txn"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/txn", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "accessTransactionGinContext" {
t.Error("wrong response body", respBody)
}
if response.Code != 200 {
t.Error("wrong response code", response.Code)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
NumErrors: 1,
})
}
func TestNilApp(t *testing.T) {
var app *newrelic.Application
router := gin.Default()
router.Use(Middleware(app))
router.GET("/hello", hello)
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "hello response" {
t.Error("wrong response body", respBody)
}
}
func errorStatus(c *gin.Context) {
c.String(500, "an error happened")
}
// The Gin.Context.Status method behavior changed with this pull
// request: https://github.com/gin-gonic/gin/pull/1606. This change
// affects our ability to instrument the response code. In Gin v1.4.0
// and below, we always recorded a 200 status, whereas with newer Gin
// versions we now correctly capture the status.
var statusFixVersion = [...]string{"1", "5"}
// Gin added the FullPath method to the Gin.Context in this version. When
// available, we use this method to set the transaction name.
var fullPathVersion = [...]string{"1", "5"}
func useFullPathVersion(v string) bool {
return checkVersionIsAtLeast(v, fullPathVersion)
}
func useStatusFixVersion(v string) bool {
return checkVersionIsAtLeast(v, statusFixVersion)
}
func checkVersionIsAtLeast(checkV string, checkAgainst [2]string) bool {
parts := strings.Split(strings.TrimPrefix(checkV, "v"), ".")
if len(parts) < 2 {
return false
}
if parts[0] < checkAgainst[0] {
return false
}
return parts[1] >= checkAgainst[1]
}
func TestStatusCodes(t *testing.T) {
// Test that we are correctly able to collect status code.
expectCode := 200
if useStatusFixVersion(gin.Version) {
expectCode = 500
}
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/err", errorStatus)
txnName := "WebTransaction/Go/GET " + pkg + ".errorStatus"
if useFullPathVersion(gin.Version) {
txnName = "WebTransaction/Go/GET /err"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/err", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "an error happened" {
t.Error("wrong response body", respBody)
}
if response.Code != 500 {
t.Error("wrong response code", response.Code)
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: map[string]interface{}{
"name": txnName,
"nr.apdexPerfZone": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
AgentAttributes: map[string]interface{}{
"httpResponseCode": expectCode,
"http.statusCode": expectCode,
"request.method": "GET",
"request.uri": "/err",
"response.headers.contentType": "text/plain; charset=utf-8",
},
}})
}
func noBody(c *gin.Context) {
c.Status(500)
}
func TestNoResponseBody(t *testing.T) {
// Test that when no response body is sent (i.e. c.Writer.Write is never
// called) that we still capture status code.
expectCode := 200
if useFullPathVersion(gin.Version) {
expectCode = 500
}
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/nobody", noBody)
txnName := "WebTransaction/Go/GET " + pkg + ".noBody"
if useFullPathVersion(gin.Version) {
txnName = "WebTransaction/Go/GET /nobody"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/nobody", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "" {
t.Error("wrong response body", respBody)
}
if response.Code != 500 {
t.Error("wrong response code", response.Code)
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: map[string]interface{}{
"name": txnName,
"nr.apdexPerfZone": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
AgentAttributes: map[string]interface{}{
"httpResponseCode": expectCode,
"http.statusCode": expectCode,
"request.method": "GET",
"request.uri": "/nobody",
},
}})
}
func TestRouteWithParams(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app.Application))
router.GET("/hello/:name/*action", hello)
txnName := "GET " + pkg + ".hello"
if useFullPathVersion(gin.Version) {
// ensure the transaction is named after the route and not the url
txnName = "GET /hello/:name/*action"
}
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/hello/world/fun", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "hello response" {
t.Error("wrong response body", respBody)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
})
}
func TestMiddlewareOldNaming(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(MiddlewareHandlerTxnNames(app.Application))
router.GET("/hello", hello)
txnName := "GET " + pkg + ".hello"
response := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(response, req)
if respBody := response.Body.String(); respBody != "hello response" {
t.Error("wrong response body", respBody)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: txnName,
IsWeb: true,
})
}
|