@@ -28,12 +28,34 @@ func newTestRouter(config Config) *gin.Engine {
28
28
return router
29
29
}
30
30
31
+ func multiGroupRouter (config Config ) * gin.Engine {
32
+ router := gin .New ()
33
+ router .Use (New (config ))
34
+
35
+ app1 := router .Group ("/app1" )
36
+ app1 .GET ("" , func (c * gin.Context ) {
37
+ c .String (http .StatusOK , "app1" )
38
+ })
39
+
40
+ app2 := router .Group ("/app2" )
41
+ app2 .GET ("" , func (c * gin.Context ) {
42
+ c .String (http .StatusOK , "app2" )
43
+ })
44
+
45
+ app3 := router .Group ("/app3" )
46
+ app3 .GET ("" , func (c * gin.Context ) {
47
+ c .String (http .StatusOK , "app3" )
48
+ })
49
+
50
+ return router
51
+ }
52
+
31
53
func performRequest (r http.Handler , method , origin string ) * httptest.ResponseRecorder {
32
- return performRequestWithHeaders (r , method , origin , http.Header {})
54
+ return performRequestWithHeaders (r , method , "/" , origin , http.Header {})
33
55
}
34
56
35
- func performRequestWithHeaders (r http.Handler , method , origin string , header http.Header ) * httptest.ResponseRecorder {
36
- req , _ := http .NewRequestWithContext (context .Background (), method , "/" , nil )
57
+ func performRequestWithHeaders (r http.Handler , method , path , origin string , header http.Header ) * httptest.ResponseRecorder {
58
+ req , _ := http .NewRequestWithContext (context .Background (), method , path , nil )
37
59
// From go/net/http/request.go:
38
60
// For incoming requests, the Host header is promoted to the
39
61
// Request.Host field and removed from the Header map.
@@ -299,6 +321,9 @@ func TestPassesAllowOrigins(t *testing.T) {
299
321
AllowOriginFunc : func (origin string ) bool {
300
322
return origin == "http://github.com"
301
323
},
324
+ AllowOriginWithContextFunc : func (c * gin.Context , origin string ) bool {
325
+ return origin == "http://sample.com"
326
+ },
302
327
})
303
328
304
329
// no CORS request, origin == ""
@@ -311,7 +336,7 @@ func TestPassesAllowOrigins(t *testing.T) {
311
336
// no CORS request, origin == host
312
337
h := http.Header {}
313
338
h .Set ("Host" , "facebook.com" )
314
- w = performRequestWithHeaders (router , "GET" , "http://facebook.com" , h )
339
+ w = performRequestWithHeaders (router , "GET" , "/" , " http://facebook.com" , h )
315
340
assert .Equal (t , "get" , w .Body .String ())
316
341
assert .Empty (t , w .Header ().Get ("Access-Control-Allow-Origin" ))
317
342
assert .Empty (t , w .Header ().Get ("Access-Control-Allow-Credentials" ))
@@ -346,6 +371,15 @@ func TestPassesAllowOrigins(t *testing.T) {
346
371
assert .Equal (t , "Content-Type,Timestamp" , w .Header ().Get ("Access-Control-Allow-Headers" ))
347
372
assert .Equal (t , "43200" , w .Header ().Get ("Access-Control-Max-Age" ))
348
373
374
+ // allowed CORS prefligh request: allowed via AllowOriginWithContextFunc
375
+ w = performRequest (router , "OPTIONS" , "http://sample.com" )
376
+ assert .Equal (t , http .StatusNoContent , w .Code )
377
+ assert .Equal (t , "http://sample.com" , w .Header ().Get ("Access-Control-Allow-Origin" ))
378
+ assert .Equal (t , "" , w .Header ().Get ("Access-Control-Allow-Credentials" ))
379
+ assert .Equal (t , "GET,POST,PUT,HEAD" , w .Header ().Get ("Access-Control-Allow-Methods" ))
380
+ assert .Equal (t , "Content-Type,Timestamp" , w .Header ().Get ("Access-Control-Allow-Headers" ))
381
+ assert .Equal (t , "43200" , w .Header ().Get ("Access-Control-Max-Age" ))
382
+
349
383
// deny CORS prefligh request
350
384
w = performRequest (router , "OPTIONS" , "http://example.com" )
351
385
assert .Equal (t , http .StatusForbidden , w .Code )
@@ -432,6 +466,48 @@ func TestWildcard(t *testing.T) {
432
466
assert .Equal (t , 200 , w .Code )
433
467
}
434
468
469
+ func TestMultiGroupRouter (t * testing.T ) {
470
+ router := multiGroupRouter (Config {
471
+ AllowMethods : []string {"GET" },
472
+ AllowOriginWithContextFunc : func (c * gin.Context , origin string ) bool {
473
+ path := c .Request .URL .Path
474
+ if strings .HasPrefix (path , "/app1" ) {
475
+ return "http://app1.example.com" == origin
476
+ }
477
+
478
+ if strings .HasPrefix (path , "/app2" ) {
479
+ return "http://app2.example.com" == origin
480
+ }
481
+
482
+ // app 3 allows all origins
483
+ return true
484
+ },
485
+ })
486
+
487
+ // allowed CORS prefligh request
488
+ emptyHeaders := http.Header {}
489
+ app1Origin := "http://app1.example.com"
490
+ app2Origin := "http://app2.example.com"
491
+ randomOrgin := "http://random.com"
492
+
493
+ // allowed CORS preflight
494
+ w := performRequestWithHeaders (router , "OPTIONS" , "/app1" , app1Origin , emptyHeaders )
495
+ assert .Equal (t , http .StatusNoContent , w .Code )
496
+
497
+ w = performRequestWithHeaders (router , "OPTIONS" , "/app2" , app2Origin , emptyHeaders )
498
+ assert .Equal (t , http .StatusNoContent , w .Code )
499
+
500
+ w = performRequestWithHeaders (router , "OPTIONS" , "/app3" , randomOrgin , emptyHeaders )
501
+ assert .Equal (t , http .StatusNoContent , w .Code )
502
+
503
+ // disallowed CORS preflight
504
+ w = performRequestWithHeaders (router , "OPTIONS" , "/app1" , randomOrgin , emptyHeaders )
505
+ assert .Equal (t , http .StatusForbidden , w .Code )
506
+
507
+ w = performRequestWithHeaders (router , "OPTIONS" , "/app2" , randomOrgin , emptyHeaders )
508
+ assert .Equal (t , http .StatusForbidden , w .Code )
509
+ }
510
+
435
511
func TestParseWildcardRules_NoWildcard (t * testing.T ) {
436
512
config := Config {
437
513
AllowOrigins : []string {
0 commit comments