@@ -83,3 +83,141 @@ func TestCORS(t *testing.T) {
83
83
h (c )
84
84
assert .Equal (t , "http://bbb.example.com" , rec .Header ().Get (echo .HeaderAccessControlAllowOrigin ))
85
85
}
86
+
87
+ func Test_allowOriginScheme (t * testing.T ) {
88
+ tests := []struct {
89
+ domain , pattern string
90
+ expected bool
91
+ }{
92
+ {
93
+ domain : "http://example.com" ,
94
+ pattern : "http://example.com" ,
95
+ expected : true ,
96
+ },
97
+ {
98
+ domain : "https://example.com" ,
99
+ pattern : "https://example.com" ,
100
+ expected : true ,
101
+ },
102
+ {
103
+ domain : "http://example.com" ,
104
+ pattern : "https://example.com" ,
105
+ expected : false ,
106
+ },
107
+ {
108
+ domain : "https://example.com" ,
109
+ pattern : "http://example.com" ,
110
+ expected : false ,
111
+ },
112
+ }
113
+
114
+ e := echo .New ()
115
+ for _ , tt := range tests {
116
+ req := httptest .NewRequest (http .MethodOptions , "/" , nil )
117
+ rec := httptest .NewRecorder ()
118
+ c := e .NewContext (req , rec )
119
+ req .Header .Set (echo .HeaderOrigin , tt .domain )
120
+ cors := CORSWithConfig (CORSConfig {
121
+ AllowOrigins : []string {tt .pattern },
122
+ })
123
+ h := cors (echo .NotFoundHandler )
124
+ h (c )
125
+
126
+ if tt .expected {
127
+ assert .Equal (t , tt .domain , rec .Header ().Get (echo .HeaderAccessControlAllowOrigin ))
128
+ } else {
129
+ assert .Equal (t , "" , rec .Header ().Get (echo .HeaderAccessControlAllowOrigin ))
130
+ }
131
+ }
132
+ }
133
+
134
+ func Test_allowOriginSubdomain (t * testing.T ) {
135
+ tests := []struct {
136
+ domain , pattern string
137
+ expected bool
138
+ }{
139
+ {
140
+ domain : "http://aaa.example.com" ,
141
+ pattern : "http://*.example.com" ,
142
+ expected : true ,
143
+ },
144
+ {
145
+ domain : "http://bbb.aaa.example.com" ,
146
+ pattern : "http://*.example.com" ,
147
+ expected : true ,
148
+ },
149
+ {
150
+ domain : "http://bbb.aaa.example.com" ,
151
+ pattern : "http://*.aaa.example.com" ,
152
+ expected : true ,
153
+ },
154
+ {
155
+ domain : "http://aaa.example.com:8080" ,
156
+ pattern : "http://*.example.com:8080" ,
157
+ expected : true ,
158
+ },
159
+
160
+ {
161
+ domain : "http://fuga.hoge.com" ,
162
+ pattern : "http://*.example.com" ,
163
+ expected : false ,
164
+ },
165
+ {
166
+ domain : "http://ccc.bbb.example.com" ,
167
+ pattern : "http://*.aaa.example.com" ,
168
+ expected : false ,
169
+ },
170
+ {
171
+ domain : `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
172
+ .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
173
+ .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\
174
+ .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com` ,
175
+ pattern : "http://*.example.com" ,
176
+ expected : false ,
177
+ },
178
+ {
179
+ domain : `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com` ,
180
+ pattern : "http://*.example.com" ,
181
+ expected : false ,
182
+ },
183
+ {
184
+ domain : "http://ccc.bbb.example.com" ,
185
+ pattern : "http://example.com" ,
186
+ expected : false ,
187
+ },
188
+ {
189
+ domain : "https://prod-preview--aaa.bbb.com" ,
190
+ pattern : "https://*--aaa.bbb.com" ,
191
+ expected : true ,
192
+ },
193
+ {
194
+ domain : "http://ccc.bbb.example.com" ,
195
+ pattern : "http://*.example.com" ,
196
+ expected : true ,
197
+ },
198
+ {
199
+ domain : "http://ccc.bbb.example.com" ,
200
+ pattern : "http://foo.[a-z]*.example.com" ,
201
+ expected : false ,
202
+ },
203
+ }
204
+
205
+ e := echo .New ()
206
+ for _ , tt := range tests {
207
+ req := httptest .NewRequest (http .MethodOptions , "/" , nil )
208
+ rec := httptest .NewRecorder ()
209
+ c := e .NewContext (req , rec )
210
+ req .Header .Set (echo .HeaderOrigin , tt .domain )
211
+ cors := CORSWithConfig (CORSConfig {
212
+ AllowOrigins : []string {tt .pattern },
213
+ })
214
+ h := cors (echo .NotFoundHandler )
215
+ h (c )
216
+
217
+ if tt .expected {
218
+ assert .Equal (t , tt .domain , rec .Header ().Get (echo .HeaderAccessControlAllowOrigin ))
219
+ } else {
220
+ assert .Equal (t , "" , rec .Header ().Get (echo .HeaderAccessControlAllowOrigin ))
221
+ }
222
+ }
223
+ }
0 commit comments