-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcors_test.go
57 lines (53 loc) · 1.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
package otils
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestCORSHeader(t *testing.T) {
tests := []struct {
name string
cors *CORS
want http.Header
}{
{
name: "with allowCredentials",
cors: &CORS{
AllowCredentials: true,
},
want: http.Header{
"Access-Control-Allow-Credentials": {"true"},
},
},
{
name: "all enabled",
cors: CORSMiddlewareAllInclusive(nil).(*CORS),
want: http.Header{
"Access-Control-Allow-Credentials": {"true"},
"Access-Control-Allow-Headers": {"*"},
"Access-Control-Allow-Methods": {"*"},
"Access-Control-Allow-Origin": {"*"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
tt.cors.setCORSForResponseWriter(rec)
res := rec.Result()
got := res.Header
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("Mismatched end headers\nGot: %s\nWant: %s", asJSON(got), asJSON(tt.want))
}
})
}
}
func asJSON(v interface{}) []byte {
blob, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
return blob
}