Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to inject a custom round tripper #252

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (u URL) MarshalYAML() (interface{}, error) {
return nil, nil
}

// RoundTripWrapper is used in HTTPClientConfig to add custom functionality to the http request path
type RoundTripWrapper func(rt http.RoundTripper) http.RoundTripper

// HTTPClientConfig configures an HTTP client.
type HTTPClientConfig struct {
// The HTTP basic authentication credentials for the targets.
Expand All @@ -92,6 +95,8 @@ type HTTPClientConfig struct {
ProxyURL URL `yaml:"proxy_url,omitempty"`
// TLSConfig to use to connect to the targets.
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
// WrapBaseRoundTripper can be used to add custom functionality in the http request path
WrapBaseRoundTripper RoundTripWrapper `yaml:"-"`
}

// SetDirectory joins any relative file paths with dir.
Expand Down Expand Up @@ -186,6 +191,10 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, disableKeepAli
}
}

if cfg.WrapBaseRoundTripper != nil {
rt = cfg.WrapBaseRoundTripper(rt)
}

// If a bearer token is provided, create a round tripper that will set the
// Authorization header correctly on each request.
if len(cfg.BearerToken) > 0 {
Expand Down
36 changes: 36 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const (
ExpectedBearer = "Bearer " + BearerToken
ExpectedUsername = "arthurdent"
ExpectedPassword = "42"
ExpectedHeader = "slartibartfast"
ExpectedHeaderValue = "fjords"
)

var invalidHTTPClientConfigs = []struct {
Expand All @@ -76,6 +78,13 @@ var invalidHTTPClientConfigs = []struct {
},
}

type roundTripperFunc func(req *http.Request) (*http.Response, error)

// RoundTrip implements the RoundTripper interface.
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
}

func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, error) {
testServer := httptest.NewUnstartedServer(http.HandlerFunc(handler))

Expand Down Expand Up @@ -104,6 +113,14 @@ func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt
}

func TestNewClientFromConfig(t *testing.T) {
wrapper := func(rt http.RoundTripper) http.RoundTripper {
return roundTripperFunc(func(req *http.Request) (*http.Response, error) {
req.Header.Add(ExpectedHeader, ExpectedHeaderValue)

return rt.RoundTrip(req)
})
}

var newClientValidConfig = []struct {
clientConfig HTTPClientConfig
handler func(w http.ResponseWriter, r *http.Request)
Expand Down Expand Up @@ -195,6 +212,25 @@ func TestNewClientFromConfig(t *testing.T) {
fmt.Fprint(w, ExpectedMessage)
}
},
}, {
clientConfig: HTTPClientConfig{
TLSConfig: TLSConfig{
CAFile: "",
CertFile: ClientCertificatePath,
KeyFile: ClientKeyNoPassPath,
ServerName: "",
InsecureSkipVerify: true},
WrapBaseRoundTripper: wrapper,
},
handler: func(w http.ResponseWriter, r *http.Request) {
val := r.Header.Get(ExpectedHeader)
if val != ExpectedHeaderValue {
fmt.Fprintf(w, "The expected Header Value (%s) differs from the obtained Header Value (%s)",
ExpectedHeaderValue, val)
} else {
fmt.Fprint(w, ExpectedMessage)
}
},
},
}

Expand Down