Skip to content

Commit

Permalink
Add transport option and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Fehr <matthias@monostream.com>
  • Loading branch information
matthiasfehr committed Feb 22, 2022
1 parent aa33f4f commit 45367ca
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
8 changes: 8 additions & 0 deletions pkg/getter/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package getter

import (
"bytes"
"net/http"
"time"

"github.com/pkg/errors"
Expand All @@ -43,6 +44,7 @@ type options struct {
version string
registryClient *registry.Client
timeout time.Duration
transport *http.Transport
}

// Option allows specifying various settings configurable by the user for overriding the defaults
Expand Down Expand Up @@ -119,6 +121,12 @@ func WithUntar() Option {
}
}

func WithTransport(transport *http.Transport) Option {
return func(opts *options) {
opts.transport = transport
}
}

// Getter is an interface to support GET to the specified URL.
type Getter interface {
// Get file content by url string
Expand Down
30 changes: 20 additions & 10 deletions pkg/getter/httpgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"net/http"
"net/url"
"sync"

"github.com/pkg/errors"

Expand All @@ -33,6 +34,7 @@ import (
type HTTPGetter struct {
opts options
transport *http.Transport
once sync.Once
}

// Get performs a Get from repo.Getter and returns the body.
Expand Down Expand Up @@ -107,11 +109,19 @@ func NewHTTPGetter(options ...Option) (Getter, error) {
}

func (g *HTTPGetter) httpClient() (*http.Client, error) {
if g.transport == nil {
g.transport = &http.Transport{
DisableCompression: true,
Proxy: http.ProxyFromEnvironment,
}
var transport *http.Transport

if g.opts.transport != nil {
transport = g.opts.transport
} else {
g.once.Do(func() {
g.transport = &http.Transport{
DisableCompression: true,
Proxy: http.ProxyFromEnvironment,
}
})

transport = g.transport
}

if (g.opts.certFile != "" && g.opts.keyFile != "") || g.opts.caFile != "" {
Expand All @@ -127,21 +137,21 @@ func (g *HTTPGetter) httpClient() (*http.Client, error) {
}
tlsConf.ServerName = sni

g.transport.TLSClientConfig = tlsConf
transport.TLSClientConfig = tlsConf
}

if g.opts.insecureSkipVerifyTLS {
if g.transport.TLSClientConfig == nil {
g.transport.TLSClientConfig = &tls.Config{
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
} else {
g.transport.TLSClientConfig.InsecureSkipVerify = true
transport.TLSClientConfig.InsecureSkipVerify = true
}
}

client := &http.Client{
Transport: g.transport,
Transport: transport,
Timeout: g.opts.timeout,
}

Expand Down
76 changes: 76 additions & 0 deletions pkg/getter/httpgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestHTTPGetter(t *testing.T) {
ca, pub, priv := join(cd, "rootca.crt"), join(cd, "crt.pem"), join(cd, "key.pem")
insecure := false
timeout := time.Second * 5
transport := &http.Transport{}

// Test with options
g, err = NewHTTPGetter(
Expand All @@ -59,6 +60,7 @@ func TestHTTPGetter(t *testing.T) {
WithTLSClientConfig(pub, priv, ca),
WithInsecureSkipVerifyTLS(insecure),
WithTimeout(timeout),
WithTransport(transport),
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -105,6 +107,10 @@ func TestHTTPGetter(t *testing.T) {
t.Errorf("Expected NewHTTPGetter to contain %s as Timeout flag, got %s", timeout, hg.opts.timeout)
}

if hg.opts.transport != transport {
t.Errorf("Expected NewHTTPGetter to contain %p as Transport, got %p", transport, hg.opts.transport)
}

// Test if setting insecureSkipVerifyTLS is being passed to the ops
insecure = true

Expand Down Expand Up @@ -443,3 +449,73 @@ func verifyInsecureSkipVerify(t *testing.T, g HTTPGetter, caseName string, expec
}
return transport
}

func TestDefaultHTTPTransportReuse(t *testing.T) {
g := HTTPGetter{}

httpClient1, err := g.httpClient()

if err != nil {
t.Fatal(err)
}

if httpClient1 == nil {
t.Fatalf("Expected non nil value for http client")
}

transport1 := (httpClient1.Transport).(*http.Transport)

httpClient2, err := g.httpClient()

if err != nil {
t.Fatal(err)
}

if httpClient2 == nil {
t.Fatalf("Expected non nil value for http client")
}

transport2 := (httpClient2.Transport).(*http.Transport)

if transport1 != transport2 {
t.Fatalf("Expected default transport to be reused")
}
}

func TestHTTPTransportOption(t *testing.T) {
transport := &http.Transport{}

g := HTTPGetter{}
g.opts.transport = transport
httpClient1, err := g.httpClient()

if err != nil {
t.Fatal(err)
}

if httpClient1 == nil {
t.Fatalf("Expected non nil value for http client")
}

transport1 := (httpClient1.Transport).(*http.Transport)

if transport1 != transport {
t.Fatalf("Expected transport option to be applied")
}

httpClient2, err := g.httpClient()

if err != nil {
t.Fatal(err)
}

if httpClient2 == nil {
t.Fatalf("Expected non nil value for http client")
}

transport2 := (httpClient2.Transport).(*http.Transport)

if transport1 != transport2 {
t.Fatalf("Expected applied transport to be reused")
}
}

0 comments on commit 45367ca

Please sign in to comment.