github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/httpx/private_ip_validator_test.go (about)

     1  package httpx
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestIsAssociatedIPAllowed(t *testing.T) {
    10  	for _, disallowed := range []string{
    11  		"localhost",
    12  		"https://localhost/foo?bar=baz#zab",
    13  		"127.0.0.0",
    14  		"127.255.255.255",
    15  		"172.16.0.0",
    16  		"172.31.255.255",
    17  		"192.168.0.0",
    18  		"192.168.255.255",
    19  		"10.0.0.0",
    20  		"10.255.255.255",
    21  		"::1",
    22  	} {
    23  		t.Run("case="+disallowed, func(t *testing.T) {
    24  			require.Error(t, DisallowIPPrivateAddresses(disallowed))
    25  		})
    26  	}
    27  }
    28  
    29  func TestDisallowLocalIPAddressesWhenSet(t *testing.T) {
    30  	require.NoError(t, DisallowIPPrivateAddresses(""))
    31  	require.Error(t, DisallowIPPrivateAddresses("127.0.0.1"))
    32  }