forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_cross_process_test.go
113 lines (102 loc) · 3.26 KB
/
internal_cross_process_test.go
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
package newrelic
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/nedscode/go-agent/internal"
"github.com/nedscode/go-agent/internal/cat"
)
var (
crossProcessReplyFn = func(reply *internal.ConnectReply) {
reply.EncodingKey = "encoding_key"
reply.CrossProcessID = "12345#67890"
reply.TrustedAccounts = map[int]struct{}{
12345: struct{}{},
}
}
)
func inboundHeaders(t *testing.T) http.Header {
app := testApp(crossProcessReplyFn, nil, t)
clientTxn := app.StartTransaction("client", nil, nil)
req, err := http.NewRequest("GET", "newrelic.com", nil)
if nil != err {
t.Fatal(err)
}
StartExternalSegment(clientTxn, req)
if "" == req.Header.Get(cat.NewRelicIDName) {
t.Fatal(req.Header.Get(cat.NewRelicIDName))
}
if "" == req.Header.Get(cat.NewRelicTxnName) {
t.Fatal(req.Header.Get(cat.NewRelicTxnName))
}
return req.Header
}
var (
inboundCrossProcessRequest = func() *http.Request {
app := testApp(crossProcessReplyFn, nil, nil)
clientTxn := app.StartTransaction("client", nil, nil)
req, err := http.NewRequest("GET", "newrelic.com", nil)
StartExternalSegment(clientTxn, req)
if "" == req.Header.Get(cat.NewRelicIDName) {
panic("missing cat header NewRelicIDName: " + req.Header.Get(cat.NewRelicIDName))
}
if "" == req.Header.Get(cat.NewRelicTxnName) {
panic("missing cat header NewRelicTxnName: " + req.Header.Get(cat.NewRelicTxnName))
}
if nil != err {
panic(err)
}
return req
}()
catIntrinsics = map[string]interface{}{
"name": "WebTransaction/Go/hello",
"nr.pathHash": "fa013f2a",
"nr.guid": internal.MatchAnything,
"nr.referringTransactionGuid": internal.MatchAnything,
"nr.referringPathHash": "41c04f7d",
"nr.apdexPerfZone": "S",
"client_cross_process_id": "12345#67890",
"nr.tripId": internal.MatchAnything,
}
)
func TestCrossProcessWriteHeaderSuccess(t *testing.T) {
// Test that the CAT response header is present when the consumer uses
// txn.WriteHeader.
app := testApp(crossProcessReplyFn, nil, t)
w := httptest.NewRecorder()
txn := app.StartTransaction("hello", w, inboundCrossProcessRequest)
txn.WriteHeader(200)
txn.End()
if "" == w.Header().Get(cat.NewRelicAppDataName) {
t.Error(w.Header().Get(cat.NewRelicAppDataName))
}
app.ExpectMetrics(t, webMetrics)
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: catIntrinsics,
AgentAttributes: map[string]interface{}{
"request.method": "GET",
"httpResponseCode": 200,
},
UserAttributes: map[string]interface{}{},
}})
}
func TestCrossProcessWriteSuccess(t *testing.T) {
// Test that the CAT response header is present when the consumer uses
// txn.Write.
app := testApp(crossProcessReplyFn, nil, t)
w := httptest.NewRecorder()
txn := app.StartTransaction("hello", w, inboundCrossProcessRequest)
txn.Write([]byte("response text"))
txn.End()
if "" == w.Header().Get(cat.NewRelicAppDataName) {
t.Error(w.Header().Get(cat.NewRelicAppDataName))
}
app.ExpectMetrics(t, webMetrics)
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: catIntrinsics,
// Do not test attributes here: In Go 1.5
// response.headers.contentType will be not be present.
AgentAttributes: nil,
UserAttributes: map[string]interface{}{},
}})
}