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
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nrgin
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
"github.com/newrelic/go-agent/internal/integrationsupport"
)
var (
pkg = "github.com/newrelic/go-agent/_integrations/nrgin/v1"
)
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))
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)
}
app.ExpectTxnMetrics(t, internal.WantTxn{
Name: pkg + ".hello",
IsWeb: true,
})
}
func TestRouterGroup(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app))
group := router.Group("/group")
group.GET("/hello", 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: pkg + ".hello",
IsWeb: true,
})
}
func TestAnonymousHandler(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app))
router.GET("/anon", func(c *gin.Context) {
c.Writer.WriteString("anonymous function handler")
})
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: pkg + ".TestAnonymousHandler.func1",
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))
router.GET("/header", multipleWriteHeader)
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: pkg + ".multipleWriteHeader",
IsWeb: true,
NumErrors: 1,
})
}
func accessTransactionGinContext(c *gin.Context) {
if txn := Transaction(c); nil != txn {
txn.NoticeError(errors.New("problem"))
}
c.Writer.WriteString("accessTransactionGinContext")
}
func TestContextTransaction(t *testing.T) {
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app))
router.GET("/txn", accessTransactionGinContext)
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: pkg + ".accessTransactionGinContext",
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")
}
func TestStatusCodes(t *testing.T) {
// Test that we are correctly able to collect status code.
// This behavior changed with this pull request: https://github.com/gin-gonic/gin/pull/1606
// In Gin v1.4.0 and below, we always recorded a 200 status, whereas with
// newer Gin versions we now correctly capture the status.
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app))
router.GET("/err", errorStatus)
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": "WebTransaction/Go/" + pkg + ".errorStatus",
"nr.apdexPerfZone": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 500,
"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.
app := integrationsupport.NewBasicTestApp()
router := gin.Default()
router.Use(Middleware(app))
router.GET("/nobody", 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": "WebTransaction/Go/" + pkg + ".noBody",
"nr.apdexPerfZone": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 500,
"request.method": "GET",
"request.uri": "/nobody",
},
}})
}
|