github.com/argoproj/argo-cd/v3@v3.2.1/util/proxy/proxy_test.go (about) 1 package proxy 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "os/exec" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestAddProxyEnvIfAbsent(t *testing.T) { 15 t.Run("Existing proxy env variables", func(t *testing.T) { 16 proxy := "https://proxy:5000" 17 noProxy := ".argoproj.io" 18 cmd := exec.Command("test") 19 cmd.Env = []string{`http_proxy="https_proxy=https://env-proxy:8888"`, "key=val", "no_proxy=.argoproj.io"} 20 got := UpsertEnv(cmd, proxy, noProxy) 21 assert.Equal(t, []string{"key=val", httpProxy(proxy), httpsProxy(proxy), noProxyVar(noProxy)}, got) 22 }) 23 t.Run("proxy env variables not found", func(t *testing.T) { 24 proxy := "http://proxy:5000" 25 noProxy := ".argoproj.io" 26 cmd := exec.Command("test") 27 cmd.Env = []string{"key=val"} 28 got := UpsertEnv(cmd, proxy, noProxy) 29 assert.Equal(t, []string{"key=val", httpProxy(proxy), httpsProxy(proxy), noProxyVar(noProxy)}, got) 30 }) 31 } 32 33 func TestGetCallBack(t *testing.T) { 34 t.Run("custom proxy present", func(t *testing.T) { 35 proxy := "http://proxy:8888" 36 url, err := GetCallback(proxy, "")(nil) 37 require.NoError(t, err) 38 assert.Equal(t, proxy, url.String()) 39 }) 40 t.Run("custom proxy present, noProxy filteres request", func(t *testing.T) { 41 proxy := "http://proxy:8888" 42 noProxy := "argoproj.io" 43 url, err := GetCallback(proxy, noProxy)(&http.Request{URL: &url.URL{Host: "argoproj.io"}}) 44 require.NoError(t, err) 45 assert.Nil(t, url) // proxy object being nil indicates that no proxy should be used for this request 46 }) 47 t.Run("custom proxy absent", func(t *testing.T) { 48 proxyEnv := "http://proxy:8888" 49 t.Setenv("http_proxy", "http://proxy:8888") 50 url, err := GetCallback("", "")(httptest.NewRequest(http.MethodGet, proxyEnv, http.NoBody)) 51 require.NoError(t, err) 52 assert.Equal(t, proxyEnv, url.String()) 53 }) 54 }