@@ -395,8 +395,9 @@ func TestRouteIsProtected(t *testing.T) {
395
395
c .ProtectRoutes = append (c .ProtectRoutes , tt .config .ProtectRoutes ... )
396
396
c .ExcludeRoutes = append (c .ExcludeRoutes , tt .config .ExcludeRoutes ... )
397
397
c .ProtectFileExtensions = append (c .ProtectFileExtensions , tt .config .ProtectFileExtensions ... )
398
- bc := & CaptchaProtect {
399
- config : c ,
398
+ bc , err := NewCaptchaProtect (context .Background (), nil , c , "captcha-protect" )
399
+ if err != nil {
400
+ t .Errorf ("unexpected error %v" , err )
400
401
}
401
402
402
403
result := bc .RouteIsProtected (tt .path )
@@ -411,7 +412,6 @@ func TestGetClientIP(t *testing.T) {
411
412
tests := []struct {
412
413
name string
413
414
config Config
414
- exemptIps []* net.IPNet
415
415
headerValue string
416
416
remoteAddr string
417
417
expectedIP string
@@ -422,7 +422,6 @@ func TestGetClientIP(t *testing.T) {
422
422
IPForwardedHeader : "X-Forwarded-For" ,
423
423
IPDepth : 0 ,
424
424
},
425
- exemptIps : []* net.IPNet {},
426
425
headerValue : "1.1.1.1, 2.2.2.2" ,
427
426
remoteAddr : "3.3.3.3:1234" ,
428
427
expectedIP : "2.2.2.2" ,
@@ -432,8 +431,8 @@ func TestGetClientIP(t *testing.T) {
432
431
config : Config {
433
432
IPForwardedHeader : "X-Forwarded-For" ,
434
433
IPDepth : 0 ,
434
+ ExemptIPs : []string {"2.2.2.2/32" },
435
435
},
436
- exemptIps : []* net.IPNet {parseCIDR ("2.2.2.2/32" , t )},
437
436
headerValue : "1.1.1.1, 3.3.3.3, 2.2.2.2" ,
438
437
remoteAddr : "3.3.3.3:1234" ,
439
438
expectedIP : "3.3.3.3" ,
@@ -444,7 +443,6 @@ func TestGetClientIP(t *testing.T) {
444
443
IPForwardedHeader : "X-Forwarded-For" ,
445
444
IPDepth : 1 ,
446
445
},
447
- exemptIps : []* net.IPNet {},
448
446
headerValue : "1.1.1.1, 2.2.2.2, 3.3.3.3, 127.0.0.1, 192.168.0.1" ,
449
447
remoteAddr : "3.3.3.3:1234" ,
450
448
expectedIP : "2.2.2.2" ,
@@ -455,7 +453,6 @@ func TestGetClientIP(t *testing.T) {
455
453
IPForwardedHeader : "X-Forwarded-For" ,
456
454
IPDepth : 1 ,
457
455
},
458
- exemptIps : []* net.IPNet {},
459
456
headerValue : "1.1.1.1, 2.2.2.2, 3.3.3.3" ,
460
457
remoteAddr : "3.3.3.3:1234" ,
461
458
expectedIP : "2.2.2.2" ,
@@ -465,8 +462,8 @@ func TestGetClientIP(t *testing.T) {
465
462
config : Config {
466
463
IPForwardedHeader : "X-Forwarded-For" ,
467
464
IPDepth : 0 ,
465
+ ExemptIPs : []string {"2.2.0.0/16" },
468
466
},
469
- exemptIps : []* net.IPNet {parseCIDR ("2.2.0.0/16" , t )},
470
467
headerValue : "127.0.0.1, 192.168.1.1, 172.16.1.2, 2.2.3.4" ,
471
468
remoteAddr : "4.4.4.4:5678" ,
472
469
expectedIP : "4.4.4.4" ,
@@ -477,7 +474,6 @@ func TestGetClientIP(t *testing.T) {
477
474
IPForwardedHeader : "X-Forwarded-For" ,
478
475
IPDepth : 0 ,
479
476
},
480
- exemptIps : []* net.IPNet {},
481
477
headerValue : "" ,
482
478
remoteAddr : "4.4.4.4:5678" ,
483
479
expectedIP : "4.4.4.4" ,
@@ -488,7 +484,6 @@ func TestGetClientIP(t *testing.T) {
488
484
IPDepth : 0 ,
489
485
IPForwardedHeader : "" ,
490
486
},
491
- exemptIps : []* net.IPNet {},
492
487
headerValue : "shouldBeIgnored" ,
493
488
remoteAddr : "5.5.5.5:4321" ,
494
489
expectedIP : "5.5.5.5" ,
@@ -506,16 +501,12 @@ func TestGetClientIP(t *testing.T) {
506
501
c := CreateConfig ()
507
502
c .IPForwardedHeader = tc .config .IPForwardedHeader
508
503
c .IPDepth = tc .config .IPDepth
509
- exemptIps := tc .exemptIps
510
- bc := & CaptchaProtect {
511
- config : c ,
512
- ipv4Mask : net .CIDRMask (16 , 32 ),
513
- ipv6Mask : net .CIDRMask (64 , 128 ),
514
- }
515
- for _ , ip := range c .ExemptIPs {
516
- exemptIps = append (exemptIps , parseCIDR (ip , t ))
504
+ c .ProtectRoutes = []string {"/" }
505
+ c .ExemptIPs = tc .config .ExemptIPs
506
+ bc , err := NewCaptchaProtect (context .Background (), nil , c , "captcha-protect" )
507
+ if err != nil {
508
+ t .Errorf ("unexpected error %v" , err )
517
509
}
518
- bc .exemptIps = exemptIps
519
510
520
511
ip , _ := bc .getClientIP (req )
521
512
if ip != tc .expectedIP {
0 commit comments