@@ -176,45 +176,102 @@ func TestMiddleware_Options_InvalidMethod(t *testing.T) {
176
176
}
177
177
178
178
func TestMiddleware_Cors (t * testing.T ) {
179
- // setup types
180
- wantOrigin := "*"
181
- wantExposeHeaders := "link, x-total-count"
182
- m := & internal.Metadata {
183
- Vela : & internal.Vela {
184
- Address : "http://localhost:8080" ,
179
+ tests := []struct {
180
+ name string
181
+ m * internal.Metadata
182
+ origin string
183
+ expectedOrigin string
184
+ expectedCredentials string
185
+ expectedExposeHeaders string
186
+ }{
187
+ {
188
+ name : "*" ,
189
+ m : & internal.Metadata {
190
+ Vela : & internal.Vela {
191
+ Address : "http://localhost:8080" ,
192
+ CorsAllowOrigins : []string {},
193
+ },
194
+ },
195
+ origin : "http://localhost:8888" ,
196
+ expectedOrigin : "*" ,
197
+ expectedCredentials : "" ,
198
+ expectedExposeHeaders : "link, x-total-count" ,
199
+ },
200
+ {
201
+ name : "WebAddress is origin" ,
202
+ m : & internal.Metadata {
203
+ Vela : & internal.Vela {
204
+ WebAddress : "http://localhost:8888" ,
205
+ CorsAllowOrigins : []string {},
206
+ },
207
+ },
208
+ origin : "http://localhost:8888" ,
209
+ expectedOrigin : "http://localhost:8888" ,
210
+ expectedCredentials : "true" ,
211
+ expectedExposeHeaders : "link, x-total-count" ,
212
+ },
213
+ {
214
+ name : "CORSAllowOrigins origin is web address" ,
215
+ m : & internal.Metadata {
216
+ Vela : & internal.Vela {
217
+ WebAddress : "http://localhost:8888" ,
218
+ CorsAllowOrigins : []string {"http://localhost:3000" , "http://localhost:3001" },
219
+ },
220
+ },
221
+ origin : "http://localhost:8888" ,
222
+ expectedOrigin : "http://localhost:8888" ,
223
+ expectedCredentials : "true" ,
224
+ expectedExposeHeaders : "link, x-total-count" ,
225
+ },
226
+ {
227
+ name : "CORSAllowOrigins origin is in list" ,
228
+ m : & internal.Metadata {
229
+ Vela : & internal.Vela {
230
+ WebAddress : "" ,
231
+ CorsAllowOrigins : []string {"http://localhost:3000" , "http://localhost:3001" , "http://localhost:8888" },
232
+ },
233
+ },
234
+ origin : "http://localhost:8888" ,
235
+ expectedOrigin : "http://localhost:8888" ,
236
+ expectedCredentials : "true" ,
237
+ expectedExposeHeaders : "link, x-total-count" ,
185
238
},
186
239
}
187
240
188
- // setup context
189
- gin .SetMode (gin .TestMode )
190
-
191
- resp := httptest .NewRecorder ()
192
- context , engine := gin .CreateTestContext (resp )
193
- context .Request , _ = http .NewRequest (http .MethodGet , "/health" , nil )
194
-
195
- // setup mock server
196
- engine .Use (Metadata (m ))
197
- engine .Use (Cors )
198
- engine .GET ("/health" , func (c * gin.Context ) {
199
- c .Status (http .StatusOK )
200
- })
201
-
202
- // run test
203
- engine .ServeHTTP (context .Writer , context .Request )
204
-
205
- gotOrigin := context .Writer .Header ().Get ("Access-Control-Allow-Origin" )
206
- gotExposeHeaders := context .Writer .Header ().Get ("Access-Control-Expose-Headers" )
207
-
208
- if resp .Code != http .StatusOK {
209
- t .Errorf ("CORS returned %v, want %v" , resp .Code , http .StatusOK )
210
- }
211
-
212
- if ! reflect .DeepEqual (gotOrigin , wantOrigin ) {
213
- t .Errorf ("CORS Access-Control-Allow-Origin is %v, want %v" , gotOrigin , wantOrigin )
214
- }
215
-
216
- if ! reflect .DeepEqual (gotExposeHeaders , wantExposeHeaders ) {
217
- t .Errorf ("CORS Access-Control-Expose-Headers is %v, want %v" , gotExposeHeaders , wantExposeHeaders )
241
+ for _ , tt := range tests {
242
+ t .Run (tt .name , func (t * testing.T ) {
243
+ gin .SetMode (gin .TestMode )
244
+ resp := httptest .NewRecorder ()
245
+ context , engine := gin .CreateTestContext (resp )
246
+ context .Request , _ = http .NewRequest (http .MethodGet , "/health" , nil )
247
+ context .Request .Header .Add ("Origin" , tt .origin )
248
+
249
+ // inject metadata
250
+ engine .Use (func (c * gin.Context ) {
251
+ c .Set ("metadata" , tt .m )
252
+ c .Next ()
253
+ })
254
+ engine .Use (Cors )
255
+ engine .GET ("/health" , func (c * gin.Context ) {
256
+ c .Status (http .StatusOK )
257
+ })
258
+ engine .ServeHTTP (context .Writer , context .Request )
259
+
260
+ gotOrigin := context .Writer .Header ().Get ("Access-Control-Allow-Origin" )
261
+ if gotOrigin != tt .expectedOrigin {
262
+ t .Errorf ("Access-Control-Allow-Origin is %v; want %v" , gotOrigin , tt .expectedOrigin )
263
+ }
264
+
265
+ gotCredentials := context .Writer .Header ().Get ("Access-Control-Allow-Credentials" )
266
+ if gotCredentials != tt .expectedCredentials {
267
+ t .Errorf ("Access-Control-Allow-Credentials is %v; want %v" , gotCredentials , tt .expectedCredentials )
268
+ }
269
+
270
+ gotExposeHeaders := context .Writer .Header ().Get ("Access-Control-Expose-Headers" )
271
+ if gotExposeHeaders != tt .expectedExposeHeaders {
272
+ t .Errorf ("Access-Control-Expose-Headers is %v; want %v" , gotExposeHeaders , tt .expectedExposeHeaders )
273
+ }
274
+ })
218
275
}
219
276
}
220
277
0 commit comments