-
Notifications
You must be signed in to change notification settings - Fork 21
/
cors_test.go
326 lines (252 loc) · 7.12 KB
/
cors_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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package cors
import (
"math/rand"
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
)
var sHeaders = "TestOne, TestTwo, TestThree, TestFour, TestFive"
var config Config = Config{
Origins: "*",
ValidateHeaders: true,
Credentials: true,
RequestHeaders: "Authorization, Content-Type, Accept",
ExposedHeaders: "Authorization",
Methods: "GET, POST",
MaxAge: 1 * time.Minute,
}
func TestPrepare(t *testing.T) {
config := Config{
Origins: "*",
ValidateHeaders: true,
Credentials: true,
RequestHeaders: "Authorization, Content-Type, Accept",
ExposedHeaders: "Authorization",
Methods: "GET, POST",
MaxAge: 1 * time.Minute,
}
config.prepare()
if len(config.requestHeaders) != 3 {
t.Fatal("Unexpected number of request headers")
}
if len(config.methods) != 2 {
t.Fatal("Unexpected number of methods.")
}
if len(config.origins) != 1 {
t.Fatal("Unexpected number of origins")
}
if config.credentials != "true" {
t.Fatal("String conversion bad?")
}
if config.maxAge != "60" {
t.Fatalf("One minute should be sixty seconds, and it should be stored as a string. Instead it is %s", config.maxAge)
}
}
func TestBadConfiguration(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Fatal("With no origin set, we should panic.")
}
}()
Middleware(Config{Origins: ""})
}
func TestNoOrigin(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
router := gin.New()
router.Use(Middleware(Config{
Origins: "http://testing.com",
}))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) != "" {
t.Fatal("This should not match.")
}
}
func TestMismatchOrigin(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
req.Header.Set("Origin", "http://files.testing.com")
router := gin.New()
router.Use(Middleware(Config{
Origins: "http://testing.com",
}))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) != "" {
t.Fatal("This should not match.")
}
}
func TestPreflightRequest(t *testing.T) {
req, _ := http.NewRequest("OPTIONS", "/", nil)
w := httptest.NewRecorder()
req.Header.Set(OriginKey, "http://files.testing.com")
req.Header.Set(RequestMethodKey, "GET")
req.Header.Set(RequestHeadersKey, "Content-Type")
req.Header.Set(RequestHeadersKey, "accept")
router := gin.New()
router.Use(Middleware(config))
router.ServeHTTP(w, req)
if w.Header().Get(AllowMethodsKey) != "GET, POST" {
t.Fatal("Mismatch of methods.")
}
if w.Header().Get(AllowHeadersKey) != "Authorization, Content-Type, Accept" {
t.Fatal("Mismatch of headers.")
}
if w.Header().Get(MaxAgeKey) != "60" {
t.Fatal("Incorrect max age.")
}
if w.Header().Get(AllowOriginKey) != "http://files.testing.com" {
t.Fatal("Incorrect origin.")
}
if w.Header().Get(AllowCredentialsKey) != "true" {
t.Fatal("Incorrect credentials value")
}
}
func TestPreflightMethodMismatch(t *testing.T) {
req, _ := http.NewRequest("OPTIONS", "/", nil)
w := httptest.NewRecorder()
req.Header.Set(OriginKey, "http://files.testing.com")
req.Header.Set(RequestMethodKey, "PUT")
router := gin.New()
router.Use(Middleware(config))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) != "" {
t.Fatal("Cors headers should not be set.")
}
}
func TestPreflightHeaderMismatch(t *testing.T) {
req, _ := http.NewRequest("OPTIONS", "/", nil)
w := httptest.NewRecorder()
req.Header.Set(OriginKey, "http://files.testing.com")
req.Header.Set(RequestMethodKey, "GET")
req.Header.Set(RequestHeadersKey, "Range")
router := gin.New()
router.Use(Middleware(config))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) != "" {
t.Fatal("Cors headers should not be set.")
}
}
func TestMatchOrigin(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
req.Header.Set("Origin", "http://files.testing.com")
router := gin.New()
router.Use(Middleware(Config{
Origins: "http://files.testing.com",
}))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) == "" {
t.Fatal("Origin matches, this header should be set.")
}
}
func TestForceOrigin(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
req.Header.Set("Origin", "http://localhost")
router := gin.New()
router.Use(Middleware(config))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) == "" {
t.Fatal("Origin always matches, this header should be set.")
}
}
func TestForceOriginCredentails(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
req.Header.Set("Origin", "http://localhost")
router := gin.New()
router.Use(Middleware(Config{
Origins: "http://localhost",
ValidateHeaders: true,
Credentials: false,
RequestHeaders: "Authorization, Content-Type, Accept",
ExposedHeaders: "Authorization",
Methods: "GET, POST",
MaxAge: 1 * time.Minute,
}))
router.ServeHTTP(w, req)
if w.Header().Get(AllowOriginKey) != "http://localhost" {
t.Fatal("Improper Origin is set.")
}
}
func TestValidateMethods(t *testing.T) {
testFailMethod := "PUT"
testPassMethod := "GET"
config := Config{
Origins: "*",
ValidateHeaders: true,
Methods: "GET, POST",
}
config.prepare()
if test := validateRequestMethod(testFailMethod, config); test {
t.Fatal("Expected to return false")
}
if test := validateRequestMethod(testPassMethod, config); !test {
t.Fatal("Expected to return true")
}
config.ValidateHeaders = false
if test := validateRequestMethod(testFailMethod, config); !test {
t.Fatal("Expected to return true, since validate is off.")
}
}
func TestValidateHeaders(t *testing.T) {
testFailHeader := "Authorization, MissingHeader"
testPassHeader := "Authorization, Accept"
config := Config{
Origins: "*",
ValidateHeaders: true,
RequestHeaders: "Authorization, Content-Type, Accept",
}
config.prepare()
if test := validateRequestHeaders(testFailHeader, config); test {
t.Fatal("Expected headers to not match, return false.")
}
if test := validateRequestHeaders(testPassHeader, config); !test {
t.Fatal("Expected headers to match, should return true.")
}
config.ValidateHeaders = false
if test := validateRequestHeaders(testFailHeader, config); !test {
t.Fatal("Expect it to always return true when not validating.")
}
}
func BenchmarkSortFive(b *testing.B) {
ssHeaders := sort.StringSlice(strings.Split(sHeaders, ", "))
sort.Sort(ssHeaders)
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := rand.Intn(10)
search := ""
switch {
case index >= 5 && index < 10:
search = "NotFound"
default:
search = ssHeaders[index]
}
if idx := sort.SearchStrings(ssHeaders, search); ssHeaders[idx] == search {
}
}
}
func BenchmarkRangeFive(b *testing.B) {
ssHeaders := strings.Split(sHeaders, ", ")
b.ResetTimer()
for i := 0; i < b.N; i++ {
index := rand.Intn(10)
search := ""
switch {
case index >= 5 && index < 10:
search = "NotFound"
default:
search = ssHeaders[index]
}
for _, value := range ssHeaders {
if value == search {
break
}
}
}
}