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

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrgorilla
     5  
     6  import (
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/gorilla/mux"
    12  	newrelic "github.com/newrelic/go-agent"
    13  	"github.com/newrelic/go-agent/internal"
    14  	"github.com/newrelic/go-agent/internal/integrationsupport"
    15  )
    16  
    17  func makeHandler(text string) http.Handler {
    18  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		w.Write([]byte(text))
    20  	})
    21  }
    22  
    23  func TestBasicRoute(t *testing.T) {
    24  	app := integrationsupport.NewBasicTestApp()
    25  	r := mux.NewRouter()
    26  	r.Handle("/alpha", makeHandler("alpha response"))
    27  	InstrumentRoutes(r, app)
    28  	response := httptest.NewRecorder()
    29  	req, err := http.NewRequest("GET", "/alpha", nil)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	r.ServeHTTP(response, req)
    34  	if respBody := response.Body.String(); respBody != "alpha response" {
    35  		t.Error("wrong response body", respBody)
    36  	}
    37  	app.ExpectTxnMetrics(t, internal.WantTxn{
    38  		Name:  "alpha",
    39  		IsWeb: true,
    40  	})
    41  }
    42  
    43  func TestSubrouterRoute(t *testing.T) {
    44  	app := integrationsupport.NewBasicTestApp()
    45  	r := mux.NewRouter()
    46  	users := r.PathPrefix("/users").Subrouter()
    47  	users.Handle("/add", makeHandler("adding user"))
    48  	InstrumentRoutes(r, app)
    49  	response := httptest.NewRecorder()
    50  	req, err := http.NewRequest("GET", "/users/add", nil)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	r.ServeHTTP(response, req)
    55  	if respBody := response.Body.String(); respBody != "adding user" {
    56  		t.Error("wrong response body", respBody)
    57  	}
    58  	app.ExpectTxnMetrics(t, internal.WantTxn{
    59  		Name:  "users/add",
    60  		IsWeb: true,
    61  	})
    62  }
    63  
    64  func TestNamedRoute(t *testing.T) {
    65  	app := integrationsupport.NewBasicTestApp()
    66  	r := mux.NewRouter()
    67  	r.Handle("/named", makeHandler("named route")).Name("special-name-route")
    68  	InstrumentRoutes(r, app)
    69  	response := httptest.NewRecorder()
    70  	req, err := http.NewRequest("GET", "/named", nil)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	r.ServeHTTP(response, req)
    75  	if respBody := response.Body.String(); respBody != "named route" {
    76  		t.Error("wrong response body", respBody)
    77  	}
    78  	app.ExpectTxnMetrics(t, internal.WantTxn{
    79  		Name:  "special-name-route",
    80  		IsWeb: true,
    81  	})
    82  }
    83  
    84  func TestRouteNotFound(t *testing.T) {
    85  	app := integrationsupport.NewBasicTestApp()
    86  	r := mux.NewRouter()
    87  	r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    88  		w.WriteHeader(500)
    89  		w.Write([]byte("not found"))
    90  	})
    91  	// Tests that routes do not get double instrumented when
    92  	// InstrumentRoutes is called twice by expecting error metrics with a
    93  	// count of 1.
    94  	InstrumentRoutes(r, app)
    95  	InstrumentRoutes(r, app)
    96  	response := httptest.NewRecorder()
    97  	req, err := http.NewRequest("GET", "/foo", nil)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	r.ServeHTTP(response, req)
   102  	if respBody := response.Body.String(); respBody != "not found" {
   103  		t.Error("wrong response body", respBody)
   104  	}
   105  	if response.Code != 500 {
   106  		t.Error("wrong response code", response.Code)
   107  	}
   108  	// Error metrics test the 500 response code capture.
   109  	app.ExpectTxnMetrics(t, internal.WantTxn{
   110  		Name:      "NotFoundHandler",
   111  		IsWeb:     true,
   112  		NumErrors: 1,
   113  	})
   114  }
   115  
   116  func TestNilApp(t *testing.T) {
   117  	var app newrelic.Application
   118  	r := mux.NewRouter()
   119  	r.Handle("/alpha", makeHandler("alpha response"))
   120  	InstrumentRoutes(r, app)
   121  	response := httptest.NewRecorder()
   122  	req, err := http.NewRequest("GET", "/alpha", nil)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	r.ServeHTTP(response, req)
   127  	if respBody := response.Body.String(); respBody != "alpha response" {
   128  		t.Error("wrong response body", respBody)
   129  	}
   130  }