github.com/lulzWill/go-agent@v2.1.2+incompatible/internal_cross_process_test.go (about) 1 package newrelic 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/lulzWill/go-agent/internal" 9 "github.com/lulzWill/go-agent/internal/cat" 10 ) 11 12 var ( 13 crossProcessReplyFn = func(reply *internal.ConnectReply) { 14 reply.EncodingKey = "encoding_key" 15 reply.CrossProcessID = "12345#67890" 16 reply.TrustedAccounts = map[int]struct{}{ 17 12345: struct{}{}, 18 } 19 } 20 ) 21 22 func inboundHeaders(t *testing.T) http.Header { 23 app := testApp(crossProcessReplyFn, nil, t) 24 clientTxn := app.StartTransaction("client", nil, nil) 25 req, err := http.NewRequest("GET", "newrelic.com", nil) 26 if nil != err { 27 t.Fatal(err) 28 } 29 StartExternalSegment(clientTxn, req) 30 if "" == req.Header.Get(cat.NewRelicIDName) { 31 t.Fatal(req.Header.Get(cat.NewRelicIDName)) 32 } 33 if "" == req.Header.Get(cat.NewRelicTxnName) { 34 t.Fatal(req.Header.Get(cat.NewRelicTxnName)) 35 } 36 return req.Header 37 } 38 39 var ( 40 inboundCrossProcessRequest = func() *http.Request { 41 app := testApp(crossProcessReplyFn, nil, nil) 42 clientTxn := app.StartTransaction("client", nil, nil) 43 req, err := http.NewRequest("GET", "newrelic.com", nil) 44 StartExternalSegment(clientTxn, req) 45 if "" == req.Header.Get(cat.NewRelicIDName) { 46 panic("missing cat header NewRelicIDName: " + req.Header.Get(cat.NewRelicIDName)) 47 } 48 if "" == req.Header.Get(cat.NewRelicTxnName) { 49 panic("missing cat header NewRelicTxnName: " + req.Header.Get(cat.NewRelicTxnName)) 50 } 51 if nil != err { 52 panic(err) 53 } 54 return req 55 }() 56 catIntrinsics = map[string]interface{}{ 57 "name": "WebTransaction/Go/hello", 58 "nr.pathHash": "fa013f2a", 59 "nr.guid": internal.MatchAnything, 60 "nr.referringTransactionGuid": internal.MatchAnything, 61 "nr.referringPathHash": "41c04f7d", 62 "nr.apdexPerfZone": "S", 63 "client_cross_process_id": "12345#67890", 64 "nr.tripId": internal.MatchAnything, 65 } 66 ) 67 68 func TestCrossProcessWriteHeaderSuccess(t *testing.T) { 69 // Test that the CAT response header is present when the consumer uses 70 // txn.WriteHeader. 71 app := testApp(crossProcessReplyFn, nil, t) 72 w := httptest.NewRecorder() 73 txn := app.StartTransaction("hello", w, inboundCrossProcessRequest) 74 txn.WriteHeader(200) 75 txn.End() 76 77 if "" == w.Header().Get(cat.NewRelicAppDataName) { 78 t.Error(w.Header().Get(cat.NewRelicAppDataName)) 79 } 80 81 app.ExpectMetrics(t, webMetrics) 82 app.ExpectTxnEvents(t, []internal.WantEvent{{ 83 Intrinsics: catIntrinsics, 84 AgentAttributes: map[string]interface{}{ 85 "request.method": "GET", 86 "httpResponseCode": 200, 87 }, 88 UserAttributes: map[string]interface{}{}, 89 }}) 90 } 91 92 func TestCrossProcessWriteSuccess(t *testing.T) { 93 // Test that the CAT response header is present when the consumer uses 94 // txn.Write. 95 app := testApp(crossProcessReplyFn, nil, t) 96 w := httptest.NewRecorder() 97 txn := app.StartTransaction("hello", w, inboundCrossProcessRequest) 98 txn.Write([]byte("response text")) 99 txn.End() 100 101 if "" == w.Header().Get(cat.NewRelicAppDataName) { 102 t.Error(w.Header().Get(cat.NewRelicAppDataName)) 103 } 104 105 app.ExpectMetrics(t, webMetrics) 106 app.ExpectTxnEvents(t, []internal.WantEvent{{ 107 Intrinsics: catIntrinsics, 108 // Do not test attributes here: In Go 1.5 109 // response.headers.contentType will be not be present. 110 AgentAttributes: nil, 111 UserAttributes: map[string]interface{}{}, 112 }}) 113 }