forked from rollbar/rollbar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry_test.go
128 lines (99 loc) · 3.82 KB
/
telemetry_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package rollbar
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewTelemetryDefault(t *testing.T) {
telemetry := NewTelemetry(nil)
assert.NotNil(t, telemetry)
expectedTelemetry := &Telemetry{Queue: NewQueue(TelemetryQueueSize)}
expectedTelemetry.Network.ScrubHeaders = regexp.MustCompile("Authorization")
assert.Equal(t, expectedTelemetry, telemetry)
}
func TestNewTelemetryWithOptions(t *testing.T) {
client := http.Client{}
telemetry := NewTelemetry(nil, SetCustomQueueSize(100), EnableNetworkTelemetry(&client),
EnableNetworkTelemetryRequestHeaders(), EnableNetworkTelemetryResponseHeaders(), EnableLoggerTelemetry())
expectedTelemetry := &Telemetry{Queue: telemetry.Queue}
expectedTelemetry.Network.ScrubHeaders = regexp.MustCompile("Authorization")
expectedTelemetry.Network.enableReqHeaders = true
expectedTelemetry.Network.enableResHeaders = true
expectedTelemetry.Network.Proxied = http.DefaultTransport
expectedTelemetry.Logger.Writer = os.Stdout
assert.Equal(t, expectedTelemetry, telemetry)
assert.Equal(t, client.Transport, expectedTelemetry)
}
func TestPopulateBody(t *testing.T) {
req := httptest.NewRequest("GET", "/some_url", nil)
req.Header.Set("Some_name", "some_value")
rec := httptest.NewRecorder()
telemetry := NewTelemetry(nil)
EnableNetworkTelemetryRequestHeaders()(telemetry)
EnableNetworkTelemetryResponseHeaders()(telemetry)
data := telemetry.populateTransporterBody(req, rec.Result())
assert.NotNil(t, data)
assert.True(t, data["timestamp_ms"].(int64) > 0)
delete(data, "timestamp_ms")
expectedBodyData := map[string]interface{}{"method": "GET", "status_code": 200, "subtype": "http",
"url": "://example.com/some_url", "request_headers": map[string]interface{}{"Some_name": "some_value"},
"response": map[string]interface{}{"headers": map[string]interface{}{}}}
expectedData := map[string]interface{}{"body": expectedBodyData, "level": "info", "source": "client", "type": "network"}
assert.Equal(t, expectedData, data)
}
func TestPopulateLoggerBody(t *testing.T) {
message := "some message"
telemetry := NewTelemetry(nil)
data := telemetry.populateLoggerBody([]byte(message))
assert.NotNil(t, data)
assert.True(t, data["timestamp_ms"].(int64) > 0)
delete(data, "timestamp_ms")
expectedData := map[string]interface{}{"body": map[string]interface{}{"message": message}, "level": "log",
"source": "client", "type": "log"}
assert.Equal(t, expectedData, data)
}
func TestRoundTrip(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Print(">request: ", r)
if r.URL.String() == "/good" {
fmt.Fprintln(w, "Hello, client")
}
}))
defer ts.Close()
client := http.Client{}
telemetry := NewTelemetry(nil, EnableNetworkTelemetry(&client))
req := httptest.NewRequest("GET", ts.URL+"/good", nil)
res, err := telemetry.RoundTrip(req)
assert.Nil(t, err)
assert.NotNil(t, res)
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err)
assert.Equal(t, "Hello, client\n", string(body))
items := telemetry.GetQueueItems()
assert.NotNil(t, items)
item := items[0].(map[string]interface{})
delete(item, "timestamp_ms")
expectedData := telemetry.populateTransporterBody(req, res)
delete(expectedData, "timestamp_ms")
assert.Equal(t, item, expectedData)
}
func TestWrite(t *testing.T) {
telemetry := NewTelemetry(nil, EnableLoggerTelemetry())
message := "some message"
count, err := telemetry.Write([]byte(message))
assert.Nil(t, err)
assert.Equal(t, count, len(message))
items := telemetry.GetQueueItems()
assert.NotNil(t, items)
item := items[0].(map[string]interface{})
delete(item, "timestamp_ms")
expectedData := telemetry.populateLoggerBody([]byte(message))
delete(expectedData, "timestamp_ms")
assert.Equal(t, item, expectedData)
}