Skip to content

Commit

Permalink
Add tests for Facade.proxyFunc to ensure correct proxy function is …
Browse files Browse the repository at this point in the history
…returned based on configuration

Signed-off-by: shimako55 <[email protected]>
  • Loading branch information
shimako55 committed Feb 20, 2024
1 parent 814a512 commit b69677d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/apiclient/http1/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,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
}
34 changes: 34 additions & 0 deletions pkg/apiclient/http1/facade_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package http1

import (
"net/http"
"net/url"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,3 +17,34 @@ func TestFacade_do(t *testing.T) {
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)
}
})
}
}

0 comments on commit b69677d

Please sign in to comment.