/
sentryfasthttp_test.go
228 lines (212 loc) · 5.6 KB
/
sentryfasthttp_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
package sentryfasthttp_test
import (
"fmt"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/getsentry/sentry-go"
sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestIntegration(t *testing.T) {
largePayload := strings.Repeat("Large", 3*1024) // 15 KB
tests := []struct {
Path string
Method string
Body string
Handler fasthttp.RequestHandler
WantEvent *sentry.Event
}{
{
Path: "/panic",
Handler: func(*fasthttp.RequestCtx) {
panic("test")
},
WantEvent: &sentry.Event{
Level: sentry.LevelFatal,
Message: "test",
Request: &sentry.Request{
URL: "http://example.com/panic",
Method: "GET",
Headers: map[string]string{
"Host": "example.com",
"User-Agent": "fasthttp",
},
},
},
},
{
Path: "/post",
Method: "POST",
Body: "payload",
Handler: func(ctx *fasthttp.RequestCtx) {
hub := sentryfasthttp.GetHubFromContext(ctx)
hub.CaptureMessage("post: " + string(ctx.Request.Body()))
},
WantEvent: &sentry.Event{
Level: sentry.LevelInfo,
Message: "post: payload",
Request: &sentry.Request{
URL: "http://example.com/post",
Method: "POST",
Data: "payload",
Headers: map[string]string{
"Host": "example.com",
"User-Agent": "fasthttp",
},
},
},
},
{
Path: "/get",
Handler: func(ctx *fasthttp.RequestCtx) {
hub := sentryfasthttp.GetHubFromContext(ctx)
hub.CaptureMessage("get")
},
WantEvent: &sentry.Event{
Level: sentry.LevelInfo,
Message: "get",
Request: &sentry.Request{
URL: "http://example.com/get",
Method: "GET",
Headers: map[string]string{
"Host": "example.com",
"User-Agent": "fasthttp",
},
},
},
},
{
Path: "/post/large",
Method: "POST",
Body: largePayload,
Handler: func(ctx *fasthttp.RequestCtx) {
hub := sentryfasthttp.GetHubFromContext(ctx)
hub.CaptureMessage(fmt.Sprintf("post: %d KB", len(ctx.Request.Body())/1024))
},
WantEvent: &sentry.Event{
Level: sentry.LevelInfo,
Message: "post: 15 KB",
Request: &sentry.Request{
URL: "http://example.com/post/large",
Method: "POST",
// Actual request body omitted because too large.
Data: "",
Headers: map[string]string{
"Host": "example.com",
"User-Agent": "fasthttp",
},
},
},
},
{
Path: "/post/body-ignored",
Method: "POST",
Body: "client sends, fasthttp always reads, SDK reports",
Handler: func(ctx *fasthttp.RequestCtx) {
hub := sentryfasthttp.GetHubFromContext(ctx)
hub.CaptureMessage("body ignored")
},
WantEvent: &sentry.Event{
Level: sentry.LevelInfo,
Message: "body ignored",
Request: &sentry.Request{
URL: "http://example.com/post/body-ignored",
Method: "POST",
// Actual request body included because fasthttp always
// reads full request body.
Data: "client sends, fasthttp always reads, SDK reports",
Headers: map[string]string{
"Host": "example.com",
"User-Agent": "fasthttp",
},
},
},
},
}
eventsCh := make(chan *sentry.Event, len(tests))
err := sentry.Init(sentry.ClientOptions{
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
eventsCh <- event
return event
},
})
if err != nil {
t.Fatal(err)
}
sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{})
ln := fasthttputil.NewInmemoryListener()
handler := func(ctx *fasthttp.RequestCtx) {
for _, tt := range tests {
if string(ctx.Path()) == tt.Path {
tt.Handler(ctx)
return
}
}
t.Errorf("Unhandled request: %#v", ctx)
}
done := make(chan struct{})
go func() {
if err := fasthttp.Serve(ln, sentryHandler.Handle(handler)); err != nil {
t.Errorf("error in Serve: %s", err)
}
close(done)
}()
c := &fasthttp.Client{
Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
},
ReadTimeout: time.Second,
WriteTimeout: time.Second,
}
var want []*sentry.Event
for _, tt := range tests {
want = append(want, tt.WantEvent)
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
req.SetHost("example.com")
req.URI().SetPath(tt.Path)
req.Header.SetMethod(tt.Method)
req.SetBodyString(tt.Body)
if err := c.Do(req, res); err != nil {
t.Fatalf("Request %q failed: %s", tt.Path, err)
}
if res.StatusCode() != http.StatusOK {
t.Errorf("Status code = %d", res.StatusCode())
}
}
if ok := sentry.Flush(time.Second); !ok {
t.Fatal("sentry.Flush timed out")
}
close(eventsCh)
var got []*sentry.Event
for e := range eventsCh {
got = append(got, e)
}
opts := cmp.Options{
cmpopts.IgnoreFields(
sentry.Event{},
"Contexts", "EventID", "Extra", "Platform", "Modules",
"Release", "Sdk", "ServerName", "Tags", "Timestamp",
),
cmpopts.IgnoreMapEntries(func(k string, v string) bool {
// fasthttp changed Content-Length behavior in
// https://github.com/valyala/fasthttp/commit/097fa05a697fc638624a14ab294f1336da9c29b0.
// fasthttp changed Content-Type behavior in
// https://github.com/valyala/fasthttp/commit/ffa0cabed8199819e372ebd2c739998914150ff2.
// Since the specific values of those headers are not
// important from the perspective of sentry-go, we
// ignore them.
return k == "Content-Length" || k == "Content-Type"
}),
}
if diff := cmp.Diff(want, got, opts); diff != "" {
t.Fatalf("Events mismatch (-want +got):\n%s", diff)
}
ln.Close()
<-done
}