github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrecho/nrecho_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrecho
     5  
     6  import (
     7  	"errors"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/labstack/echo"
    13  	"github.com/newrelic/go-agent/internal"
    14  	"github.com/newrelic/go-agent/internal/integrationsupport"
    15  )
    16  
    17  func TestBasicRoute(t *testing.T) {
    18  	app := integrationsupport.NewBasicTestApp()
    19  
    20  	e := echo.New()
    21  	e.Use(Middleware(app))
    22  	e.GET("/hello", func(c echo.Context) error {
    23  		return c.Blob(http.StatusOK, "text/html", []byte("Hello, World!"))
    24  	})
    25  
    26  	response := httptest.NewRecorder()
    27  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	e.ServeHTTP(response, req)
    33  	if respBody := response.Body.String(); respBody != "Hello, World!" {
    34  		t.Error("wrong response body", respBody)
    35  	}
    36  	app.ExpectTxnMetrics(t, internal.WantTxn{
    37  		Name:  "hello",
    38  		IsWeb: true,
    39  	})
    40  	app.ExpectTxnEvents(t, []internal.WantEvent{{
    41  		Intrinsics: map[string]interface{}{
    42  			"name":             "WebTransaction/Go/hello",
    43  			"nr.apdexPerfZone": "S",
    44  		},
    45  		AgentAttributes: map[string]interface{}{
    46  			"httpResponseCode":             "200",
    47  			"request.method":               "GET",
    48  			"response.headers.contentType": "text/html",
    49  			"request.uri":                  "/hello",
    50  		},
    51  		UserAttributes: map[string]interface{}{},
    52  	}})
    53  }
    54  
    55  func TestNilApp(t *testing.T) {
    56  	e := echo.New()
    57  	e.Use(Middleware(nil))
    58  	e.GET("/hello", func(c echo.Context) error {
    59  		return c.String(http.StatusOK, "Hello, World!")
    60  	})
    61  
    62  	response := httptest.NewRecorder()
    63  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	e.ServeHTTP(response, req)
    69  	if respBody := response.Body.String(); respBody != "Hello, World!" {
    70  		t.Error("wrong response body", respBody)
    71  	}
    72  }
    73  
    74  func TestTransactionContext(t *testing.T) {
    75  	app := integrationsupport.NewBasicTestApp()
    76  
    77  	e := echo.New()
    78  	e.Use(Middleware(app))
    79  	e.GET("/hello", func(c echo.Context) error {
    80  		txn := FromContext(c)
    81  		if nil != txn {
    82  			txn.NoticeError(errors.New("ooops"))
    83  		}
    84  		return c.String(http.StatusOK, "Hello, World!")
    85  	})
    86  
    87  	response := httptest.NewRecorder()
    88  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	e.ServeHTTP(response, req)
    94  	if respBody := response.Body.String(); respBody != "Hello, World!" {
    95  		t.Error("wrong response body", respBody)
    96  	}
    97  	app.ExpectTxnMetrics(t, internal.WantTxn{
    98  		Name:      "hello",
    99  		IsWeb:     true,
   100  		NumErrors: 1,
   101  	})
   102  }
   103  
   104  func TestNotFoundHandler(t *testing.T) {
   105  	app := integrationsupport.NewBasicTestApp()
   106  
   107  	e := echo.New()
   108  	e.Use(Middleware(app))
   109  
   110  	response := httptest.NewRecorder()
   111  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	e.ServeHTTP(response, req)
   117  	app.ExpectTxnMetrics(t, internal.WantTxn{
   118  		Name:  "NotFoundHandler",
   119  		IsWeb: true,
   120  	})
   121  }
   122  
   123  func TestMethodNotAllowedHandler(t *testing.T) {
   124  	app := integrationsupport.NewBasicTestApp()
   125  
   126  	e := echo.New()
   127  	e.Use(Middleware(app))
   128  	e.GET("/hello", func(c echo.Context) error {
   129  		return c.String(http.StatusOK, "Hello, World!")
   130  	})
   131  
   132  	response := httptest.NewRecorder()
   133  	req, err := http.NewRequest("POST", "/hello?remove=me", nil)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	e.ServeHTTP(response, req)
   139  	app.ExpectTxnMetrics(t, internal.WantTxn{
   140  		Name:      "MethodNotAllowedHandler",
   141  		IsWeb:     true,
   142  		NumErrors: 1,
   143  	})
   144  }
   145  
   146  func TestReturnsHTTPError(t *testing.T) {
   147  	app := integrationsupport.NewBasicTestApp()
   148  
   149  	e := echo.New()
   150  	e.Use(Middleware(app))
   151  	e.GET("/hello", func(c echo.Context) error {
   152  		return echo.NewHTTPError(http.StatusTeapot, "I'm a teapot!")
   153  	})
   154  
   155  	response := httptest.NewRecorder()
   156  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  
   161  	e.ServeHTTP(response, req)
   162  	app.ExpectTxnMetrics(t, internal.WantTxn{
   163  		Name:      "hello",
   164  		IsWeb:     true,
   165  		NumErrors: 1,
   166  	})
   167  	app.ExpectTxnEvents(t, []internal.WantEvent{{
   168  		Intrinsics: map[string]interface{}{
   169  			"name":             "WebTransaction/Go/hello",
   170  			"nr.apdexPerfZone": "F",
   171  		},
   172  		AgentAttributes: map[string]interface{}{
   173  			"httpResponseCode": "418",
   174  			"request.method":   "GET",
   175  			"request.uri":      "/hello",
   176  		},
   177  		UserAttributes: map[string]interface{}{},
   178  	}})
   179  }
   180  
   181  func TestReturnsError(t *testing.T) {
   182  	app := integrationsupport.NewBasicTestApp()
   183  
   184  	e := echo.New()
   185  	e.Use(Middleware(app))
   186  	e.GET("/hello", func(c echo.Context) error {
   187  		return errors.New("ooooooooops")
   188  	})
   189  
   190  	response := httptest.NewRecorder()
   191  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	e.ServeHTTP(response, req)
   197  	app.ExpectTxnMetrics(t, internal.WantTxn{
   198  		Name:      "hello",
   199  		IsWeb:     true,
   200  		NumErrors: 1,
   201  	})
   202  	app.ExpectTxnEvents(t, []internal.WantEvent{{
   203  		Intrinsics: map[string]interface{}{
   204  			"name":             "WebTransaction/Go/hello",
   205  			"nr.apdexPerfZone": "F",
   206  		},
   207  		AgentAttributes: map[string]interface{}{
   208  			"httpResponseCode": "500",
   209  			"request.method":   "GET",
   210  			"request.uri":      "/hello",
   211  		},
   212  		UserAttributes: map[string]interface{}{},
   213  	}})
   214  }
   215  
   216  func TestResponseCode(t *testing.T) {
   217  	app := integrationsupport.NewBasicTestApp()
   218  
   219  	e := echo.New()
   220  	e.Use(Middleware(app))
   221  	e.GET("/hello", func(c echo.Context) error {
   222  		return c.Blob(http.StatusTeapot, "text/html", []byte("Hello, World!"))
   223  	})
   224  
   225  	response := httptest.NewRecorder()
   226  	req, err := http.NewRequest("GET", "/hello?remove=me", nil)
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	e.ServeHTTP(response, req)
   232  	app.ExpectTxnMetrics(t, internal.WantTxn{
   233  		Name:      "hello",
   234  		IsWeb:     true,
   235  		NumErrors: 1,
   236  	})
   237  	app.ExpectTxnEvents(t, []internal.WantEvent{{
   238  		Intrinsics: map[string]interface{}{
   239  			"name":             "WebTransaction/Go/hello",
   240  			"nr.apdexPerfZone": "F",
   241  		},
   242  		AgentAttributes: map[string]interface{}{
   243  			"httpResponseCode":             "418",
   244  			"request.method":               "GET",
   245  			"response.headers.contentType": "text/html",
   246  			"request.uri":                  "/hello",
   247  		},
   248  		UserAttributes: map[string]interface{}{},
   249  	}})
   250  }