-
Notifications
You must be signed in to change notification settings - Fork 3
/
breadcrumb_transport_test.go
132 lines (105 loc) · 3.82 KB
/
breadcrumb_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
package logger
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/getsentry/sentry-go"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type BreadcrumbTransportSuite struct {
suite.Suite
ctrl *gomock.Controller
ts *httptest.Server
hub *sentry.Hub
sendEventMock *gomock.Call
}
func (suite *BreadcrumbTransportSuite) SetupSuite() {
suite.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
}
func (suite *BreadcrumbTransportSuite) TearDownSuite() {
suite.ts.Close()
}
func (suite *BreadcrumbTransportSuite) SetupTest() {
suite.ctrl = gomock.NewController(suite.T())
transportMock := NewMockTransport(suite.ctrl)
transportMock.EXPECT().
Configure(gomock.AssignableToTypeOf(sentry.ClientOptions{})).
Return()
suite.sendEventMock = transportMock.EXPECT().
SendEvent(gomock.AssignableToTypeOf(&sentry.Event{})).
Return().
MinTimes(0)
transportMock.EXPECT().
Flush(gomock.Any()).
Return(true).
MinTimes(0)
client, err := sentry.NewClient(sentry.ClientOptions{Transport: transportMock})
suite.Require().NoError(err)
suite.hub = sentry.NewHub(client, sentry.NewScope())
}
func (suite *BreadcrumbTransportSuite) TearDownTest() {
suite.ctrl.Finish()
}
func (suite *BreadcrumbTransportSuite) TestNew() {
suite.T().Run("fallback to default transport", func(t *testing.T) {
transport := NewBreadcrumbTransport(sentry.LevelDebug, nil)
require.Equal(t, transport.(*breadcrumbTransport).Transport, http.DefaultTransport)
})
}
func (suite *BreadcrumbTransportSuite) TestRoundTripSuccess() {
suite.sendEventMock.Do(func(event *sentry.Event) {
suite.Require().Len(event.Breadcrumbs, 1, "event should have one breadcrumb")
breadcrumb := event.Breadcrumbs[0]
suite.Equal(BreadcrumbTypeHTTP, breadcrumb.Type, "breadcrumb should have http type")
expectedData := map[string]interface{}{
BreadcrumbDataMethod: "GET",
BreadcrumbDataReason: "204 No Content",
BreadcrumbDataStatusCode: 204,
BreadcrumbDataURL: suite.ts.URL,
}
suite.Equal(expectedData, breadcrumb.Data, "breadcrumb should have data about http request")
}).MinTimes(1)
client := http.Client{
Transport: NewBreadcrumbTransport(sentry.LevelDebug, nil),
}
ctx := WithHub(context.Background(), suite.hub)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, suite.ts.URL, nil)
suite.Require().NoError(err)
resp, err := client.Do(req)
suite.Require().NoError(err, "request should be success")
defer resp.Body.Close()
suite.hub.CaptureMessage("test event")
suite.hub.Flush(1 * time.Second)
}
func (suite *BreadcrumbTransportSuite) TestRoundTripFailure() {
suite.sendEventMock.Do(func(event *sentry.Event) {
suite.Require().Len(event.Breadcrumbs, 1, "event should have one breadcrumb")
breadcrumb := event.Breadcrumbs[0]
suite.Equal(BreadcrumbTypeHTTP, breadcrumb.Type, "breadcrumb should have http type")
suite.Equal("dial tcp 127.0.0.1:21: connect: connection refused", breadcrumb.Message)
expectedData := map[string]interface{}{
BreadcrumbDataMethod: "GET",
BreadcrumbDataURL: "http://127.0.0.1:21",
}
suite.Equal(expectedData, breadcrumb.Data, "breadcrumb should have data about http request")
}).MinTimes(1)
client := http.Client{
Transport: NewBreadcrumbTransport(sentry.LevelDebug, nil),
}
ctx := WithHub(context.Background(), suite.hub)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://127.0.0.1:21", nil)
suite.Require().NoError(err)
_, err = client.Do(req) //nolint:bodyclose
suite.Require().Error(err, "request should not be success")
suite.hub.CaptureMessage("test event")
suite.hub.Flush(1 * time.Second)
}
func TestBreadcrumbTransport(t *testing.T) {
suite.Run(t, new(BreadcrumbTransportSuite))
}