forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
120 lines (97 loc) · 3.29 KB
/
client_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
package sentry
import (
"errors"
"testing"
)
func TestNewClientAllowsEmptyDSN(t *testing.T) {
transport := &TransportMock{}
client, err := NewClient(ClientOptions{
Transport: transport,
})
if err != nil {
t.Fatalf("expected no error when creating client without a DNS but got %v", err)
}
client.CaptureException(errors.New("custom error"), nil, &ScopeMock{})
assertEqual(t, transport.lastEvent.Exception[0].Value, "custom error")
}
type customComplexError struct {
Message string
}
func (e customComplexError) Error() string {
return "customComplexError: " + e.Message
}
func (e customComplexError) AnswerToLife() string {
return "42"
}
func setupClientTest() (*Client, *ScopeMock, *TransportMock) {
scope := &ScopeMock{}
transport := &TransportMock{}
client, _ := NewClient(ClientOptions{
Dsn: "http://[email protected]/1337",
Transport: transport,
Integrations: func(i []Integration) []Integration {
return []Integration{}
},
})
return client, scope, transport
}
func TestCaptureMessageShouldSendEventWithProvidedMessage(t *testing.T) {
client, scope, transport := setupClientTest()
client.CaptureMessage("foo", nil, scope)
assertEqual(t, transport.lastEvent.Message, "foo")
}
func TestCaptureExceptionShouldSendEventWithProvidedError(t *testing.T) {
client, scope, transport := setupClientTest()
client.CaptureException(errors.New("custom error"), nil, scope)
assertEqual(t, transport.lastEvent.Exception[0].Value, "custom error")
}
func TestCaptureExceptionShouldNotFailWhenPassedNil(t *testing.T) {
client, scope, transport := setupClientTest()
client.CaptureException(nil, nil, scope)
assertEqual(t, transport.lastEvent.Message, "Called CaptureException with nil value")
}
func TestCaptureEventShouldSendEventWithProvidedError(t *testing.T) {
client, scope, transport := setupClientTest()
event := NewEvent()
event.Message = "event message"
client.CaptureEvent(event, nil, scope)
assertEqual(t, transport.lastEvent.Message, "event message")
}
func TestSampleRateCanDropEvent(t *testing.T) {
client, scope, transport := setupClientTest()
client.options.SampleRate = 0.000000000000001
client.CaptureMessage("Foo", nil, scope)
if transport.lastEvent != nil {
t.Error("expected event to be dropped")
}
}
func TestApplyToScopeCanDropEvent(t *testing.T) {
client, scope, transport := setupClientTest()
scope.shouldDropEvent = true
client.CaptureMessage("Foo", nil, scope)
if transport.lastEvent != nil {
t.Error("expected event to be dropped")
}
}
func TestBeforeSendCanDropEvent(t *testing.T) {
client, scope, transport := setupClientTest()
client.options.BeforeSend = func(event *Event, hint *EventHint) *Event {
return nil
}
client.CaptureMessage("Foo", nil, scope)
if transport.lastEvent != nil {
t.Error("expected event to be dropped")
}
}
func TestBeforeSendGetAccessToEventHint(t *testing.T) {
client, scope, transport := setupClientTest()
client.options.BeforeSend = func(event *Event, hint *EventHint) *Event {
if ex, ok := hint.OriginalException.(customComplexError); ok {
event.Message = event.Exception[0].Value + " " + ex.AnswerToLife()
}
return event
}
ex := customComplexError{Message: "Foo"}
client.CaptureException(ex, &EventHint{OriginalException: ex}, scope)
assertEqual(t, transport.lastEvent.Message, "customComplexError: Foo 42")
}