forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_set_web_response_test.go
105 lines (96 loc) · 3.08 KB
/
internal_set_web_response_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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/newrelic/go-agent/internal"
)
func TestTransactionStartedWithoutResponse(t *testing.T) {
// Test that the http.ResponseWriter methods of the transaction can
// safely be called if a ResponseWriter is not provided.
app := testApp(nil, nil, t)
txn := app.StartTransaction("hello", nil, nil)
txn.WriteHeader(123)
if hdr := txn.Header(); hdr != nil {
t.Error(hdr)
}
n, err := txn.Write([]byte("should not panic"))
if err != nil || n != 0 {
t.Error(err, n)
}
txn.End()
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{"httpResponseCode": 123},
Intrinsics: map[string]interface{}{"name": "OtherTransaction/Go/hello"},
}})
}
func TestSetWebResponseNil(t *testing.T) {
// Test that the http.ResponseWriter methods of the transaction can
// safely be called if txn.SetWebResponse(nil) has been called.
app := testApp(nil, nil, t)
txn := app.StartTransaction("hello", nil, nil)
txn = txn.SetWebResponse(nil)
txn.WriteHeader(123)
if hdr := txn.Header(); hdr != nil {
t.Error(hdr)
}
n, err := txn.Write([]byte("should not panic"))
if err != nil || n != 0 {
t.Error(err, n)
}
txn.End()
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{"httpResponseCode": 123},
Intrinsics: map[string]interface{}{"name": "OtherTransaction/Go/hello"},
}})
}
func TestSetWebResponseSuccess(t *testing.T) {
// Test that the http.ResponseWriter methods of the transaction use the
// writer set by SetWebResponse.
app := testApp(nil, nil, t)
txn := app.StartTransaction("hello", nil, nil)
w := httptest.NewRecorder()
txn = txn.SetWebResponse(w)
txn.WriteHeader(123)
hdr := txn.Header()
hdr.Set("zip", "zap")
body := "should not panic"
n, err := txn.Write([]byte(body))
if err != nil || n != len(body) {
t.Error(err, n)
}
txn.End()
if w.Code != 123 {
t.Error(w.Code)
}
if w.HeaderMap.Get("zip") != "zap" {
t.Error(w.HeaderMap)
}
if w.Body.String() != body {
t.Error(w.Body.String())
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{"httpResponseCode": 123},
Intrinsics: map[string]interface{}{"name": "OtherTransaction/Go/hello"},
}})
}
type writerWithFlush struct{}
func (w writerWithFlush) Header() http.Header { return nil }
func (w writerWithFlush) WriteHeader(int) {}
func (w writerWithFlush) Write([]byte) (int, error) { return 0, nil }
func (w writerWithFlush) Flush() {}
func TestSetWebResponseTxnUpgraded(t *testing.T) {
// Test that the using Transaction reference returned by SetWebResponse
// properly has the optional methods that the ResponseWriter does.
app := testApp(nil, nil, t)
txn := app.StartTransaction("hello", nil, nil)
if _, ok := txn.(http.Flusher); ok {
t.Error("should not have Flusher")
}
txn = txn.SetWebResponse(writerWithFlush{})
if _, ok := txn.(http.Flusher); !ok {
t.Error("should have Flusher now")
}
}