-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcors_test.go
171 lines (156 loc) · 6.41 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
package bahamut
import (
"net/http"
"testing"
// nolint:revive // Allow dot imports for readability in tests
. "github.com/smartystreets/goconvey/convey"
)
func TestNewDefaultCORSAccessControlPolicy(t *testing.T) {
Convey("Calling NewDefaultCORSAccessControlPolicy should work", t, func() {
c := NewDefaultCORSController("origin.com", []string{"additionalorigin.com"})
ac := c.PolicyForRequest(nil)
So(ac.AllowOrigin, ShouldEqual, "origin.com")
So(ac.additionalOrigins, ShouldResemble, map[string]struct{}{"additionalorigin.com": {}})
So(ac.AllowCredentials, ShouldBeTrue)
So(ac.MaxAge, ShouldEqual, 1500)
So(ac.AllowHeaders, ShouldResemble, []string{
"Authorization",
"Accept",
"Content-Type",
"Cache-Control",
"Cookie",
"If-Modified-Since",
"X-Requested-With",
"X-Count-Total",
"X-Namespace",
"X-External-Tracking-Type",
"X-External-Tracking-ID",
"X-TLS-Client-Certificate",
"Accept-Encoding",
"X-Fields",
"X-Read-Consistency",
"X-Write-Consistency",
"Idempotency-Key",
})
So(ac.AllowMethods, ShouldResemble, []string{
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
"OPTIONS",
})
So(ac.ExposeHeaders, ShouldResemble, []string{
"X-Requested-With",
"X-Count-Total",
"X-Namespace",
"X-Messages",
"X-Fields",
"X-Next",
})
})
}
func TestCORSInject(t *testing.T) {
Convey("Calling inject with no http.Heade should work", t, func() {
a := NewDefaultCORSController("origin", nil)
ac := a.PolicyForRequest(nil)
So(func() { ac.Inject(nil, "", false) }, ShouldNotPanic)
})
Convey("Calling inject without passing request origin should work", t, func() {
a := NewDefaultCORSController("origin", nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "origin")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with request prefligh should work", t, func() {
a := NewDefaultCORSController("origin", nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "", true) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldEqual, "1500")
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "origin")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with matching origin should work", t, func() {
a := NewDefaultCORSController("origin", nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "origin", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "origin")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with non matching origin should work", t, func() {
a := NewDefaultCORSController("origin", nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "notorigin", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "origin")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with matching additional origin should work", t, func() {
a := NewDefaultCORSController("origin", []string{"additional.com"})
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "additional.com", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "additional.com")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with * configured", t, func() {
a := NewDefaultCORSController("*", []string{"additional.com"})
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "additional.com", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "*")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "")
})
Convey("Calling inject with mirroring configured and passed origin", t, func() {
a := NewDefaultCORSController(CORSOriginMirror, nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "hello.com", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "hello.com")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "true")
})
Convey("Calling inject with mirroring configured and no passed origin", t, func() {
a := NewDefaultCORSController(CORSOriginMirror, nil)
ac := a.PolicyForRequest(nil)
h := http.Header{}
So(func() { ac.Inject(h, "", false) }, ShouldNotPanic)
So(h.Get("Access-Control-Allow-Headers"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Methods"), ShouldBeEmpty)
So(h.Get("Access-Control-Max-Age"), ShouldBeEmpty)
So(h.Get("Access-Control-Allow-Origin"), ShouldEqual, "")
So(h.Get("Access-Control-Expose-Headers"), ShouldNotBeEmpty)
So(h.Get("Access-Control-Allow-Credentials"), ShouldEqual, "")
})
}