forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsn_test.go
192 lines (176 loc) · 5.63 KB
/
dsn_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
package sentry
import (
"encoding/json"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
type DsnTest struct {
in string
dsn *Dsn // expected value after parsing
url string // expected Store API URL
envURL string // expected Envelope API URL
}
var dsnTests = map[string]DsnTest{
"AllFields": {
in: "https://public:secret@domain:8888/foo/bar/42",
dsn: &Dsn{
scheme: schemeHTTPS,
publicKey: "public",
secretKey: "secret",
host: "domain",
port: 8888,
path: "/foo/bar",
projectID: "42",
},
url: "https://domain:8888/foo/bar/api/42/store/",
envURL: "https://domain:8888/foo/bar/api/42/envelope/",
},
"MinimalSecure": {
in: "https://public@domain/42",
dsn: &Dsn{
scheme: schemeHTTPS,
publicKey: "public",
host: "domain",
port: 443,
projectID: "42",
},
url: "https://domain/api/42/store/",
envURL: "https://domain/api/42/envelope/",
},
"MinimalInsecure": {
in: "http://public@domain/42",
dsn: &Dsn{
scheme: schemeHTTP,
publicKey: "public",
host: "domain",
port: 80,
projectID: "42",
},
url: "http://domain/api/42/store/",
envURL: "http://domain/api/42/envelope/",
},
}
//nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestNewDsn(t *testing.T) {
for name, tt := range dsnTests {
t.Run(name, func(t *testing.T) {
dsn, err := NewDsn(tt.in)
if err != nil {
t.Fatalf("NewDsn() error: %q", err)
}
// Internal fields
if diff := cmp.Diff(tt.dsn, dsn, cmp.AllowUnexported(Dsn{})); diff != "" {
t.Errorf("NewDsn() mismatch (-want +got):\n%s", diff)
}
// Store API URL
url := dsn.StoreAPIURL().String()
if diff := cmp.Diff(tt.url, url); diff != "" {
t.Errorf("dsn.StoreAPIURL() mismatch (-want +got):\n%s", diff)
}
// Envelope API URL
url = dsn.EnvelopeAPIURL().String()
if diff := cmp.Diff(tt.envURL, url); diff != "" {
t.Errorf("dsn.EnvelopeAPIURL() mismatch (-want +got):\n%s", diff)
}
})
}
}
type invalidDsnTest struct {
in string
err string // expected substring of the error
}
var invalidDsnTests = map[string]invalidDsnTest{
"Empty": {"", "invalid scheme"},
"NoScheme1": {"public:secret@:8888/42", "invalid scheme"},
// FIXME: NoScheme2's error message is inconsistent with NoScheme1; consider
// avoiding leaking errors from url.Parse.
"NoScheme2": {"://public:secret@:8888/42", "missing protocol scheme"},
"NoPublicKey": {"https://:secret@domain:8888/42", "empty username"},
"NoHost": {"https://public:secret@:8888/42", "empty host"},
"NoProjectID1": {"https://public:secret@domain:8888/", "empty project id"},
"NoProjectID2": {"https://public:secret@domain:8888", "empty project id"},
"BadURL": {"!@#$%^&*()", "invalid url"},
"BadScheme": {"ftp://public:secret@domain:8888/1", "invalid scheme"},
"BadPort": {"https://public:secret@domain:wat/42", "invalid port"},
"TrailingSlash": {"https://public:secret@domain:8888/42/", "empty project id"},
}
//nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestNewDsnInvalidInput(t *testing.T) {
for name, tt := range invalidDsnTests {
t.Run(name, func(t *testing.T) {
_, err := NewDsn(tt.in)
if err == nil {
t.Fatalf("got nil, want error with %q", tt.err)
}
if _, ok := err.(*DsnParseError); !ok {
t.Errorf("got %T, want %T", err, (*DsnParseError)(nil))
}
if !strings.Contains(err.Error(), tt.err) {
t.Errorf("%q does not contain %q", err.Error(), tt.err)
}
})
}
}
func TestDsnSerializeDeserialize(t *testing.T) {
url := "https://public:secret@domain:8888/foo/bar/42"
dsn, dsnErr := NewDsn(url)
serialized, _ := json.Marshal(dsn)
var deserialized Dsn
unmarshalErr := json.Unmarshal(serialized, &deserialized)
if unmarshalErr != nil {
t.Error("expected dsn unmarshal to not return error")
}
if dsnErr != nil {
t.Error("expected NewDsn to not return error")
}
assertEqual(t, `"https://public:secret@domain:8888/foo/bar/42"`, string(serialized))
assertEqual(t, url, deserialized.String())
}
func TestDsnDeserializeInvalidJSON(t *testing.T) {
var invalidJSON Dsn
invalidJSONErr := json.Unmarshal([]byte(`"whoops`), &invalidJSON)
var invalidDsn Dsn
invalidDsnErr := json.Unmarshal([]byte(`"http://wat"`), &invalidDsn)
if invalidJSONErr == nil {
t.Error("expected dsn unmarshal to return error")
}
if invalidDsnErr == nil {
t.Error("expected dsn unmarshal to return error")
}
}
func TestRequestHeadersWithoutSecretKey(t *testing.T) {
url := "https://public@domain/42"
dsn, err := NewDsn(url)
if err != nil {
t.Fatal(err)
}
headers := dsn.RequestHeaders()
authRegexp := regexp.MustCompile("^Sentry sentry_version=7, sentry_timestamp=\\d+, " +
"sentry_client=sentry.go/.+, sentry_key=public$")
if len(headers) != 2 {
t.Error("expected request to have 2 headers")
}
assertEqual(t, "application/json", headers["Content-Type"])
if authRegexp.FindStringIndex(headers["X-Sentry-Auth"]) == nil {
t.Error("expected auth header to fulfill provided pattern")
}
}
func TestRequestHeadersWithSecretKey(t *testing.T) {
url := "https://public:secret@domain/42"
dsn, err := NewDsn(url)
if err != nil {
t.Fatal(err)
}
headers := dsn.RequestHeaders()
authRegexp := regexp.MustCompile("^Sentry sentry_version=7, sentry_timestamp=\\d+, " +
"sentry_client=sentry.go/.+, sentry_key=public, sentry_secret=secret$")
if len(headers) != 2 {
t.Error("expected request to have 2 headers")
}
assertEqual(t, "application/json", headers["Content-Type"])
if authRegexp.FindStringIndex(headers["X-Sentry-Auth"]) == nil {
t.Error("expected auth header to fulfill provided pattern")
}
}