forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
292 lines (245 loc) · 6.74 KB
/
transport_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package sentry
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
)
type unserializableType struct {
UnsupportedField func()
}
const basicEvent = "{\"message\":\"mkey\",\"sdk\":{},\"user\":{}}"
const enhancedEvent = "{\"extra\":{\"info\":\"Original event couldn't be marshalled. Succeeded by stripping " +
"the data that uses interface{} type. Please verify that the data you attach to the scope is serializable.\"}," +
"\"message\":\"mkey\",\"sdk\":{},\"user\":{}}"
func TestGetRequestBodyFromEventValid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
})
got := string(body)
want := basicEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidBreadcrumbsField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Breadcrumbs: []*Breadcrumb{{
Data: map[string]interface{}{
"wat": unserializableType{},
},
}},
})
got := string(body)
want := enhancedEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidExtraField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Extra: map[string]interface{}{
"wat": unserializableType{},
},
})
got := string(body)
want := enhancedEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidContextField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Contexts: map[string]interface{}{
"wat": unserializableType{},
},
})
got := string(body)
want := enhancedEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventMultipleInvalidFields(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Breadcrumbs: []*Breadcrumb{{
Data: map[string]interface{}{
"wat": unserializableType{},
},
}},
Extra: map[string]interface{}{
"wat": unserializableType{},
},
Contexts: map[string]interface{}{
"wat": unserializableType{},
},
})
got := string(body)
want := enhancedEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventCompletelyInvalid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Exception: []Exception{{
Stacktrace: &Stacktrace{
Frames: []Frame{{
Vars: map[string]interface{}{
"wat": unserializableType{},
},
}},
},
}},
})
if body != nil {
t.Error("expected body to be nil")
}
}
func TestRetryAfterNoHeader(t *testing.T) {
r := http.Response{}
assertEqual(t, retryAfter(time.Now(), &r), time.Second*60)
}
func TestRetryAfterIncorrectHeader(t *testing.T) {
r := http.Response{
Header: map[string][]string{
"Retry-After": {"x"},
},
}
assertEqual(t, retryAfter(time.Now(), &r), time.Second*60)
}
func TestRetryAfterDelayHeader(t *testing.T) {
r := http.Response{
Header: map[string][]string{
"Retry-After": {"1337"},
},
}
assertEqual(t, retryAfter(time.Now(), &r), time.Second*1337)
}
func TestRetryAfterDateHeader(t *testing.T) {
now, _ := time.Parse(time.RFC1123, "Wed, 21 Oct 2015 07:28:00 GMT")
r := http.Response{
Header: map[string][]string{
"Retry-After": {"Wed, 21 Oct 2015 07:28:13 GMT"},
},
}
assertEqual(t, retryAfter(now, &r), time.Second*13)
}
// A testHTTPServer counts events sent to it. It requires a call to Unblock
// before incrementing its internal counter and sending a response to the HTTP
// client. This allows for coordinating the execution flow when needed.
type testHTTPServer struct {
*httptest.Server
// eventCounter counts the number of events processed by the server.
eventCounter *uint64
// ch is used to block/unblock the server on demand.
ch chan bool
}
func newTestHTTPServer(t *testing.T) *testHTTPServer {
ch := make(chan bool)
eventCounter := new(uint64)
handler := func(w http.ResponseWriter, r *http.Request) {
var event struct {
EventID string `json:"event_id"`
}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&event)
if err != nil {
t.Fatal(err)
}
// Block until signal to continue.
<-ch
count := atomic.AddUint64(eventCounter, 1)
t.Logf("[SERVER] {%.4s} event received (#%d)", event.EventID, count)
}
return &testHTTPServer{
Server: httptest.NewTLSServer(http.HandlerFunc(handler)),
eventCounter: eventCounter,
ch: ch,
}
}
func (ts *testHTTPServer) EventCount() uint64 {
return atomic.LoadUint64(ts.eventCounter)
}
func (ts *testHTTPServer) Unblock() {
ts.ch <- true
}
func TestHTTPTransport(t *testing.T) {
server := newTestHTTPServer(t)
defer server.Close()
transport := NewHTTPTransport()
transport.Configure(ClientOptions{
Dsn: fmt.Sprintf("https://test@%s/1", server.Listener.Addr()),
HTTPClient: server.Client(),
})
// Helpers
transportSendTestEvent := func(t *testing.T) (id string) {
t.Helper()
e := NewEvent()
id = uuid()
e.EventID = EventID(id)
transport.SendEvent(e)
t.Logf("[CLIENT] {%.4s} event sent", e.EventID)
return id
}
transportMustFlush := func(t *testing.T, id string) {
t.Helper()
ok := transport.Flush(100 * time.Millisecond)
if !ok {
t.Fatalf("[CLIENT] {%.4s} Flush() timed out", id)
}
}
serverEventCountMustBe := func(t *testing.T, n uint64) {
t.Helper()
count := server.EventCount()
if count != n {
t.Fatalf("[SERVER] event count = %d, want %d", count, n)
}
}
// Actual tests
testSendSingleEvent := func(t *testing.T) {
// Sending a single event should increase the server event count by
// exactly one.
initialCount := server.EventCount()
id := transportSendTestEvent(t)
// Server is blocked waiting for us, right now count must not have
// changed yet.
serverEventCountMustBe(t, initialCount)
// After unblocking the server, Flush must guarantee that the server
// event count increased by one.
server.Unblock()
transportMustFlush(t, id)
serverEventCountMustBe(t, initialCount+1)
}
t.Run("SendSingleEvent", testSendSingleEvent)
t.Run("FlushMultipleTimes", func(t *testing.T) {
// Flushing multiple times should not increase the server event count.
initialCount := server.EventCount()
for i := 0; i < 10; i++ {
transportMustFlush(t, fmt.Sprintf("loop%d", i))
}
serverEventCountMustBe(t, initialCount)
})
t.Run("ConcurrentSendAndFlush", func(t *testing.T) {
// It should be safe to send events and flush concurrently.
var wg sync.WaitGroup
wg.Add(2)
go func() {
testSendSingleEvent(t)
wg.Done()
}()
go func() {
transportMustFlush(t, "from goroutine")
wg.Done()
}()
wg.Wait()
})
}