github.com/argoproj/argo-cd/v3@v3.2.1/util/grpc/grpc_test.go (about) 1 package grpc 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 var proxyEnvKeys = []string{"ALL_PROXY", "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"} 13 14 func clearProxyEnv(t *testing.T) { 15 t.Helper() 16 for _, k := range proxyEnvKeys { 17 t.Setenv(k, "") 18 } 19 } 20 21 func applyProxyEnv(t *testing.T, envs map[string]string) { 22 t.Helper() 23 for k, v := range envs { 24 t.Setenv(k, v) 25 } 26 } 27 28 func TestBlockingDial_ProxyEnvironmentHandling(t *testing.T) { 29 tests := []struct { 30 name string 31 proxyEnv map[string]string 32 address string 33 expectError bool 34 }{ 35 { 36 name: "No proxy environment variables", 37 proxyEnv: map[string]string{}, 38 address: "127.0.0.1:8080", 39 expectError: true, 40 }, 41 { 42 name: "ALL_PROXY environment variable set", 43 proxyEnv: map[string]string{ 44 "ALL_PROXY": "http://proxy.example.com:8080", 45 }, 46 address: "remote.example.com:443", 47 expectError: true, 48 }, 49 { 50 name: "HTTP_PROXY environment variable set", 51 proxyEnv: map[string]string{ 52 "HTTP_PROXY": "http://proxy.example.com:3128", 53 }, 54 address: "api.example.com:80", 55 expectError: true, 56 }, 57 { 58 name: "HTTPS_PROXY environment variable set", 59 proxyEnv: map[string]string{ 60 "HTTPS_PROXY": "https://secure-proxy.example.com:8080", 61 }, 62 address: "secure.example.com:443", 63 expectError: true, 64 }, 65 { 66 name: "NO_PROXY bypass configuration", 67 proxyEnv: map[string]string{ 68 "ALL_PROXY": "http://proxy.example.com:8080", 69 "NO_PROXY": "localhost,127.0.0.1,*.local", 70 }, 71 address: "127.0.0.1:8080", 72 expectError: true, 73 }, 74 { 75 name: "Multiple proxy environment variables", 76 proxyEnv: map[string]string{ 77 "ALL_PROXY": "socks5://all-proxy.example.com:1080", 78 "HTTP_PROXY": "http://http-proxy.example.com:8080", 79 "HTTPS_PROXY": "https://https-proxy.example.com:8080", 80 "NO_PROXY": "localhost,*.local", 81 }, 82 address: "external.example.com:443", 83 expectError: true, 84 }, 85 } 86 87 for _, tt := range tests { 88 tt := tt 89 t.Run(tt.name, func(t *testing.T) { 90 clearProxyEnv(t) 91 applyProxyEnv(t, tt.proxyEnv) 92 93 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 94 defer cancel() 95 96 conn, err := BlockingNewClient(ctx, "tcp", tt.address, nil) 97 98 if tt.expectError { 99 require.Error(t, err) 100 assert.Nil(t, conn) 101 } else { 102 require.NoError(t, err) 103 assert.NotNil(t, conn) 104 require.NoError(t, conn.Close()) 105 } 106 }) 107 } 108 }