Skip to content

Commit

Permalink
Adding 'ExtraHeaders' field on HTTPClientConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
alanprot committed Sep 16, 2021
1 parent 8d1c9f8 commit 69d2d46
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ type HTTPClientConfig struct {
// The omitempty flag is not set, because it would be hidden from the
// marshalled configuration when set to false.
FollowRedirects bool `yaml:"follow_redirects" json:"follow_redirects"`
// Extra Headers to be added on the Http Request
ExtraHeaders map[string]string `yaml:"extra_headers,omitempty" json:"extra_headers,omitempty"`
}

// SetDirectory joins any relative file paths with dir.
Expand Down Expand Up @@ -418,6 +420,10 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, cfg.BasicAuth.PasswordFile, rt)
}

if cfg.ExtraHeaders != nil && len(cfg.ExtraHeaders) > 0 {
rt = NewExtraHeadersRoundTripper(cfg.ExtraHeaders, rt)
}

if cfg.OAuth2 != nil {
rt = NewOAuth2RoundTripper(cfg.OAuth2, rt)
}
Expand All @@ -438,6 +444,26 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
return NewTLSRoundTripper(tlsConfig, cfg.TLSConfig.CAFile, newRT)
}

type extraHeadersRoundTripper struct {
extraHeaders map[string]string
rt http.RoundTripper
}

func (rt *extraHeadersRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = cloneRequest(req)
if len(rt.extraHeaders) > 0 {
for key, value := range rt.extraHeaders {
req.Header.Set(key, value)
}
}
return rt.rt.RoundTrip(req)
}

// NewExtraHeadersRoundTripper adds the provided headers to a request
func NewExtraHeadersRoundTripper(extraHeaders map[string]string, rt http.RoundTripper) http.RoundTripper {
return &extraHeadersRoundTripper{extraHeaders, rt}
}

type authorizationCredentialsRoundTripper struct {
authType string
authCredentials Secret
Expand Down
22 changes: 22 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const (
ExpectedPassword = "42"
)

var expectedExtraHeaders = map[string]string{"Header1": "Value1", "Header2": "Value2"}

var invalidHTTPClientConfigs = []struct {
httpClientConfigFile string
errMsg string
Expand Down Expand Up @@ -197,6 +199,26 @@ func TestNewClientFromConfig(t *testing.T) {
fmt.Fprint(w, ExpectedMessage)
}
},
},
{
clientConfig: HTTPClientConfig{
ExtraHeaders: expectedExtraHeaders,
TLSConfig: TLSConfig{
CAFile: TLSCAChainPath,
CertFile: ClientCertificatePath,
KeyFile: ClientKeyNoPassPath,
ServerName: "",
InsecureSkipVerify: false},
},
handler: func(w http.ResponseWriter, r *http.Request) {
for key, value := range expectedExtraHeaders {
if r.Header.Get(key) != value {
fmt.Fprintf(w, "The received Headers (%s) does not contain all expected headers (%s).", r.Header, expectedExtraHeaders)
return
}
}
fmt.Fprint(w, ExpectedMessage)
},
}, {
clientConfig: HTTPClientConfig{
BearerTokenFile: BearerTokenFile,
Expand Down

0 comments on commit 69d2d46

Please sign in to comment.