forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsn_test.go
210 lines (177 loc) · 5.82 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
package sentry
import (
"encoding/json"
"regexp"
"testing"
)
func TestDsnParsing(t *testing.T) {
url := "https://username:password@domain:8888/foo/bar/23"
dsn, err := NewDsn(url)
if err != nil {
t.Error("expected dsn to be correctly created")
}
assertEqual(t, schemeHTTPS, dsn.scheme)
assertEqual(t, dsn.publicKey, "username")
assertEqual(t, dsn.secretKey, "password")
assertEqual(t, dsn.host, "domain")
assertEqual(t, dsn.path, "/foo/bar")
assertEqual(t, dsn.String(), url)
assertEqual(t, dsn.port, 8888)
assertEqual(t, dsn.projectID, 23)
}
func TestDsnDefaultPort(t *testing.T) {
dsn, _ := NewDsn("https://u@d:1337/23")
assertEqual(t, dsn.port, 1337)
dsn, _ = NewDsn("https://u@d/23")
assertEqual(t, dsn.port, 443)
dsn, _ = NewDsn("http://u@d/23")
assertEqual(t, dsn.port, 80)
}
func TestDsnSerializeDeserialize(t *testing.T) {
url := "https://username:password@domain:8888/foo/bar/23"
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://username:password@domain:8888/foo/bar/23\"", 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 TestValidDsnInsecure(t *testing.T) {
url := "http://username@domain:8888/42"
dsn, err := NewDsn(url)
if err != nil {
t.Error("expected dsn to be correctly created")
}
assertEqual(t, url, dsn.String())
}
func TestValidDsnNoPort(t *testing.T) {
url := "http://username@domain/42"
dsn, err := NewDsn(url)
if err != nil {
t.Error("expected dsn to be correctly created")
}
assertEqual(t, 80, dsn.port)
assertEqual(t, url, dsn.String())
assertEqual(t, "http://domain/api/42/store/", dsn.StoreAPIURL().String())
}
func TestValidDsnInsecureNoPort(t *testing.T) {
url := "https://username@domain/42"
dsn, err := NewDsn(url)
if err != nil {
t.Error("expected dsn to be correctly created")
}
assertEqual(t, 443, dsn.port)
assertEqual(t, url, dsn.String())
assertEqual(t, "https://domain/api/42/store/", dsn.StoreAPIURL().String())
}
func TestValidDsnNoPassword(t *testing.T) {
url := "https://username@domain:8888/42"
dsn, err := NewDsn(url)
if err != nil {
t.Error("expected dsn to be correctly created")
}
assertEqual(t, url, dsn.String())
assertEqual(t, "https://domain:8888/api/42/store/", dsn.StoreAPIURL().String())
}
func TestInvalidDsnInvalidUrl(t *testing.T) {
_, err := NewDsn("!@#$%^&*()")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "invalid url")
}
func TestInvalidDsnInvalidScheme(t *testing.T) {
_, err := NewDsn("ftp://username:password@domain:8888/1")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "invalid scheme")
}
func TestInvalidDsnNoUsername(t *testing.T) {
_, err := NewDsn("https://:password@domain:8888/23")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "empty username")
}
func TestInvalidDsnNoHost(t *testing.T) {
_, err := NewDsn("https://username:password@:8888/42")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "empty host")
}
func TestInvalidDsnInvalidPort(t *testing.T) {
_, err := NewDsn("https://username:password@domain:wat/42")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "invalid port")
}
func TestInvalidDsnNoProjectId(t *testing.T) {
_, err := NewDsn("https://username:password@domain:8888/")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "empty project id")
}
func TestInvalidDsnInvalidProjectId(t *testing.T) {
_, err := NewDsn("https://username:password@domain:8888/wbvdf7^W#$")
_, ok := err.(*DsnParseError)
if ok != true {
t.Error("expected error to be of type DsnParseError")
}
assertStringContains(t, err.Error(), "invalid project id")
}
func TestRequestHeadersWithoutPassword(t *testing.T) {
url := "https://username@domain:8888/23"
dsn, _ := NewDsn(url)
headers := dsn.RequestHeaders()
authRegexp := regexp.MustCompile("^Sentry sentry_version=7, sentry_timestamp=\\d+, " +
"sentry_client=sentry.go/.+, sentry_key=username$")
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 TestRequestHeadersWithPassword(t *testing.T) {
url := "https://username:secret@domain:8888/23"
dsn, _ := NewDsn(url)
headers := dsn.RequestHeaders()
authRegexp := regexp.MustCompile("^Sentry sentry_version=7, sentry_timestamp=\\d+, " +
"sentry_client=sentry.go/.+, sentry_key=username, 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")
}
}