-
Notifications
You must be signed in to change notification settings - Fork 213
/
dsn_test.go
303 lines (280 loc) · 7.59 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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)
}
url := dsn.GetAPIURL().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")
}
}
func TestGetScheme(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"http://public:secret@domain/42", "http"},
{"https://public:secret@domain/42", "https"},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetScheme(), tt.want)
}
}
func TestGetPublicKey(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"https://public:secret@domain/42", "public"},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetPublicKey(), tt.want)
}
}
func TestGetSecretKey(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"https://public:secret@domain/42", "secret"},
{"https://public@domain/42", ""},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetSecretKey(), tt.want)
}
}
func TestGetHost(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"http://public:secret@domain/42", "domain"},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetHost(), tt.want)
}
}
func TestGetPort(t *testing.T) {
tests := []struct {
dsn string
want int
}{
{"https://public:secret@domain/42", 443},
{"http://public:secret@domain/42", 80},
{"https://public:secret@domain:3000/42", 3000},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetPort(), tt.want)
}
}
func TestGetPath(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"https://public:secret@domain/42", ""},
{"https://public:secret@domain/foo/bar/42", "/foo/bar"},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetPath(), tt.want)
}
}
func TestGetProjectID(t *testing.T) {
tests := []struct {
dsn string
want string
}{
{"https://public:secret@domain/42", "42"},
}
for _, tt := range tests {
dsn, err := NewDsn(tt.dsn)
if err != nil {
t.Fatal(err)
}
assertEqual(t, dsn.GetProjectID(), tt.want)
}
}