github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrgin/v1/nrgin_context_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  // +build go1.7
     5  
     6  package nrgin
     7  
     8  import (
     9  	"context"
    10  	"errors"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"testing"
    14  
    15  	"github.com/gin-gonic/gin"
    16  	newrelic "github.com/newrelic/go-agent"
    17  	"github.com/newrelic/go-agent/internal"
    18  	"github.com/newrelic/go-agent/internal/integrationsupport"
    19  )
    20  
    21  func accessTransactionContextContext(c *gin.Context) {
    22  	var ctx context.Context = c
    23  	// Transaction is designed to take both a context.Context and a
    24  	// *gin.Context.
    25  	if txn := Transaction(ctx); nil != txn {
    26  		txn.NoticeError(errors.New("problem"))
    27  	}
    28  	c.Writer.WriteString("accessTransactionContextContext")
    29  }
    30  
    31  func TestContextContextTransaction(t *testing.T) {
    32  	app := integrationsupport.NewBasicTestApp()
    33  	router := gin.Default()
    34  	router.Use(Middleware(app))
    35  	router.GET("/txn", accessTransactionContextContext)
    36  
    37  	response := httptest.NewRecorder()
    38  	req, err := http.NewRequest("GET", "/txn", nil)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	router.ServeHTTP(response, req)
    43  	if respBody := response.Body.String(); respBody != "accessTransactionContextContext" {
    44  		t.Error("wrong response body", respBody)
    45  	}
    46  	if response.Code != 200 {
    47  		t.Error("wrong response code", response.Code)
    48  	}
    49  	app.ExpectTxnMetrics(t, internal.WantTxn{
    50  		Name:      pkg + ".accessTransactionContextContext",
    51  		IsWeb:     true,
    52  		NumErrors: 1,
    53  	})
    54  }
    55  
    56  func accessTransactionFromContext(c *gin.Context) {
    57  	// This tests that FromContext will find the transaction added to a
    58  	// *gin.Context and by nrgin.Middleware.
    59  	if txn := newrelic.FromContext(c); nil != txn {
    60  		txn.NoticeError(errors.New("problem"))
    61  	}
    62  	c.Writer.WriteString("accessTransactionFromContext")
    63  }
    64  
    65  func TestFromContext(t *testing.T) {
    66  	app := integrationsupport.NewBasicTestApp()
    67  	router := gin.Default()
    68  	router.Use(Middleware(app))
    69  	router.GET("/txn", accessTransactionFromContext)
    70  
    71  	response := httptest.NewRecorder()
    72  	req, err := http.NewRequest("GET", "/txn", nil)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	router.ServeHTTP(response, req)
    77  	if respBody := response.Body.String(); respBody != "accessTransactionFromContext" {
    78  		t.Error("wrong response body", respBody)
    79  	}
    80  	if response.Code != 200 {
    81  		t.Error("wrong response code", response.Code)
    82  	}
    83  	app.ExpectTxnMetrics(t, internal.WantTxn{
    84  		Name:      pkg + ".accessTransactionFromContext",
    85  		IsWeb:     true,
    86  		NumErrors: 1,
    87  	})
    88  }
    89  
    90  func TestContextWithoutTransaction(t *testing.T) {
    91  	txn := Transaction(context.Background())
    92  	if txn != nil {
    93  		t.Error("didn't expect a transaction", txn)
    94  	}
    95  	ctx := context.WithValue(context.Background(), internal.TransactionContextKey, 123)
    96  	txn = Transaction(ctx)
    97  	if txn != nil {
    98  		t.Error("didn't expect a transaction", txn)
    99  	}
   100  }
   101  
   102  func TestNewContextTransaction(t *testing.T) {
   103  	// This tests that nrgin.Transaction will find a transaction added to
   104  	// to a context using newrelic.NewContext.
   105  	app := integrationsupport.NewBasicTestApp()
   106  	txn := app.StartTransaction("name", nil, nil)
   107  	ctx := newrelic.NewContext(context.Background(), txn)
   108  	if tx := Transaction(ctx); nil != tx {
   109  		tx.NoticeError(errors.New("problem"))
   110  	}
   111  	txn.End()
   112  
   113  	app.ExpectTxnMetrics(t, internal.WantTxn{
   114  		Name:      "name",
   115  		IsWeb:     false,
   116  		NumErrors: 1,
   117  	})
   118  }