diff --git a/pkg/apiclient/http1/facade.go b/pkg/apiclient/http1/facade.go index 3b74b98c062c..9c92142fec95 100644 --- a/pkg/apiclient/http1/facade.go +++ b/pkg/apiclient/http1/facade.go @@ -191,3 +191,10 @@ func parseHeaders(headerStrings []string) (http.Header, error) { } return headers, nil } + +func (h *Facade) proxyFunc() func(*http.Request) (*url.URL, error) { + if h.proxy != nil { + return h.proxy + } + return http.ProxyFromEnvironment +} diff --git a/pkg/apiclient/http1/facade_test.go b/pkg/apiclient/http1/facade_test.go index 0340661e64fc..6dd238ce5233 100644 --- a/pkg/apiclient/http1/facade_test.go +++ b/pkg/apiclient/http1/facade_test.go @@ -1,6 +1,9 @@ package http1 import ( + "net/http" + "net/url" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -14,3 +17,34 @@ func TestFacade_do(t *testing.T) { require.NoError(t, err) assert.Equal(t, "http://my-url/my-ns/?labels.foo=1", u.String()) } + +func TestFacade_proxyFunc(t *testing.T) { + proxyFunc := func(_ *http.Request) (*url.URL, error) { + return nil, nil + } + tests := []struct { + name string + proxy func(*http.Request) (*url.URL, error) + want func(*http.Request) (*url.URL, error) + }{ + { + name: "use proxy settings from environment variables", + proxy: nil, + want: http.ProxyFromEnvironment, + }, + { + name: "use specific proxy", + proxy: proxyFunc, + want: proxyFunc, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := Facade{proxy: tt.want} + got := f.proxyFunc() + if reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() { + t.Errorf("Facade.proxyURL() = %p, want %p", got, tt.want) + } + }) + } +}