Skip to content

Commit c414f4d

Browse files
committed
Expose Host property in HTTPClientConfig
1 parent ef08658 commit c414f4d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

config/http_config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ type HTTPClientConfig struct {
319319
// The omitempty flag is not set, because it would be hidden from the
320320
// marshalled configuration when set to false.
321321
EnableHTTP2 bool `yaml:"enable_http2" json:"enable_http2"`
322+
// HTTPHost optionally overrides the Host header to send.
323+
// If empty, the host from the URL is used.
324+
HTTPHost string `yaml:"http_host,omitempty" json:"host,omitempty"`
322325
// Proxy configuration.
323326
ProxyConfig `yaml:",inline"`
324327
// HTTPHeaders specify headers to inject in the requests. Those headers
@@ -664,6 +667,8 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
664667

665668
if opts.host != "" {
666669
rt = NewHostRoundTripper(opts.host, rt)
670+
} else if cfg.HTTPHost != "" {
671+
rt = NewHostRoundTripper(cfg.HTTPHost, rt)
667672
}
668673

669674
// Return a new configured RoundTripper.

config/http_config_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,33 @@ func TestHost(t *testing.T) {
17451745
}))
17461746
defer ts.Close()
17471747

1748+
config := DefaultHTTPClientConfig
1749+
config.HTTPHost = "localhost.localdomain"
1750+
1751+
rt, err := NewRoundTripperFromConfig(config, "test_host")
1752+
if err != nil {
1753+
t.Fatal(err)
1754+
}
1755+
1756+
client := http.Client{
1757+
Transport: rt,
1758+
}
1759+
_, err = client.Get(ts.URL)
1760+
if err != nil {
1761+
t.Fatal(err)
1762+
}
1763+
}
1764+
1765+
func TestHostWithNewRoundTripperFromConfig(t *testing.T) {
1766+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1767+
if r.Host != "localhost.localdomain" {
1768+
t.Fatalf("Expected Host header in request to be 'localhost.localdomain', got '%s'", r.Host)
1769+
}
1770+
1771+
w.Header().Add("Content-Type", "application/json")
1772+
}))
1773+
defer ts.Close()
1774+
17481775
config := DefaultHTTPClientConfig
17491776

17501777
rt, err := NewRoundTripperFromConfig(config, "test_host", WithHost("localhost.localdomain"))

0 commit comments

Comments
 (0)