@@ -1388,10 +1388,14 @@ func TestContextAbortWithError(t *testing.T) {
13881388 assert .True (t , c .IsAborted ())
13891389}
13901390
1391+ func resetTrustedCIDRs (c * Context ) {
1392+ c .engine .trustedCIDRs , _ = c .engine .prepareTrustedCIDRs ()
1393+ }
1394+
13911395func TestContextClientIP (t * testing.T ) {
13921396 c , _ := CreateTestContext (httptest .NewRecorder ())
13931397 c .Request , _ = http .NewRequest ("POST" , "/" , nil )
1394-
1398+ resetTrustedCIDRs ( c )
13951399 resetContextForClientIPTests (c )
13961400
13971401 // Legacy tests (validating that the defaults don't break the
@@ -1421,46 +1425,56 @@ func TestContextClientIP(t *testing.T) {
14211425
14221426 // No trusted proxies
14231427 c .engine .TrustedProxies = []string {}
1428+ resetTrustedCIDRs (c )
14241429 c .engine .RemoteIPHeaders = []string {"X-Forwarded-For" }
14251430 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14261431
14271432 // Last proxy is trusted, but the RemoteAddr is not
14281433 c .engine .TrustedProxies = []string {"30.30.30.30" }
1434+ resetTrustedCIDRs (c )
14291435 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14301436
14311437 // Only trust RemoteAddr
14321438 c .engine .TrustedProxies = []string {"40.40.40.40" }
1439+ resetTrustedCIDRs (c )
14331440 assert .Equal (t , "20.20.20.20" , c .ClientIP ())
14341441
14351442 // All steps are trusted
14361443 c .engine .TrustedProxies = []string {"40.40.40.40" , "30.30.30.30" , "20.20.20.20" }
1444+ resetTrustedCIDRs (c )
14371445 assert .Equal (t , "20.20.20.20" , c .ClientIP ())
14381446
14391447 // Use CIDR
14401448 c .engine .TrustedProxies = []string {"40.40.25.25/16" , "30.30.30.30" }
1449+ resetTrustedCIDRs (c )
14411450 assert .Equal (t , "20.20.20.20" , c .ClientIP ())
14421451
14431452 // Use hostname that resolves to all the proxies
14441453 c .engine .TrustedProxies = []string {"foo" }
1454+ resetTrustedCIDRs (c )
14451455 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14461456
14471457 // Use hostname that returns an error
14481458 c .engine .TrustedProxies = []string {"bar" }
1459+ resetTrustedCIDRs (c )
14491460 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14501461
14511462 // X-Forwarded-For has a non-IP element
14521463 c .engine .TrustedProxies = []string {"40.40.40.40" }
1464+ resetTrustedCIDRs (c )
14531465 c .Request .Header .Set ("X-Forwarded-For" , " blah " )
14541466 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14551467
14561468 // Result from LookupHost has non-IP element. This should never
14571469 // happen, but we should test it to make sure we handle it
14581470 // gracefully.
14591471 c .engine .TrustedProxies = []string {"baz" }
1472+ resetTrustedCIDRs (c )
14601473 c .Request .Header .Set ("X-Forwarded-For" , " 30.30.30.30 " )
14611474 assert .Equal (t , "40.40.40.40" , c .ClientIP ())
14621475
14631476 c .engine .TrustedProxies = []string {"40.40.40.40" }
1477+ resetTrustedCIDRs (c )
14641478 c .Request .Header .Del ("X-Forwarded-For" )
14651479 c .engine .RemoteIPHeaders = []string {"X-Forwarded-For" , "X-Real-IP" }
14661480 assert .Equal (t , "10.10.10.10" , c .ClientIP ())
0 commit comments