forked from digitalmint/go-sentry-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentry_test.go
78 lines (67 loc) · 1.92 KB
/
sentry_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
package sentry
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestRedactDSN(t *testing.T) {
result := RedactDSN([]byte(`{"a":"b", "dsn":"https://[email protected]/123"}`))
if string(result) != `{"a":"b", "dsn":"REDACTED"}` {
t.Errorf("Redact test failed")
}
}
type roundTripFunc func(r *http.Request) (*http.Response, error)
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return s(r)
}
func TestRoundTrip(t *testing.T) {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, client")
w.WriteHeader(400)
}))
defer ts.Close()
client := ts.Client()
transport := client.Transport
client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
lsf := NewLogSentrySendFailures(transport)
return lsf.RoundTrip(r)
})
res, err := client.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
greeting, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(greeting) != "Hello, client" {
t.Fatal("not greeted", string(greeting))
}
}
type testErr struct{}
func (te testErr) Error() string {
return "test error"
}
func TestUnwrapToSpecificError(t *testing.T) {
d := defaultFilterErrorTypes
if errStr := unwrapToSpecificError(errors.New("test"), d); *errStr != "errorString" {
t.Errorf("unexpected %s", *errStr)
}
if errStr := unwrapToSpecificError(fmt.Errorf("test"), d); *errStr != "errorString" {
t.Errorf("unexpected %s", *errStr)
}
if errStr := unwrapToSpecificError(http.ErrAbortHandler, d); *errStr != "errorString" {
t.Errorf("unexpected %s", *errStr)
}
if errStr := unwrapToSpecificError(testErr{}, d); *errStr != "sentry.testErr" {
t.Errorf("unexpected %s", *errStr)
}
wrapped := fmt.Errorf("%w", testErr{})
if errStr := unwrapToSpecificError(wrapped, d); *errStr != "sentry.testErr" {
t.Errorf("unexpected %s", *errStr)
}
}