Skip to content

Commit

Permalink
Expose Host property in HTTPClientConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Jun 6, 2024
1 parent ef08658 commit 0bd6bdd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ type HTTPClientConfig struct {
// The omitempty flag is not set, because it would be hidden from the
// marshalled configuration when set to false.
EnableHTTP2 bool `yaml:"enable_http2" json:"enable_http2"`
// HTTPHost optionally overrides the Host header to send.
// If empty, the host from the URL is used.
HTTPHost string `yaml:"http_host,omitempty" json:"host,omitempty"`
// Proxy configuration.
ProxyConfig `yaml:",inline"`
// HTTPHeaders specify headers to inject in the requests. Those headers
Expand Down Expand Up @@ -664,6 +667,8 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon

if opts.host != "" {
rt = NewHostRoundTripper(opts.host, rt)
} else if cfg.HTTPHost != "" {
rt = NewHostRoundTripper(cfg.HTTPHost, rt)
}

// Return a new configured RoundTripper.
Expand Down
27 changes: 27 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,33 @@ func TestHost(t *testing.T) {
}))
defer ts.Close()

config := DefaultHTTPClientConfig
config.HTTPHost = "localhost.localdomain"

rt, err := NewRoundTripperFromConfig(config, "test_host")
if err != nil {
t.Fatal(err)
}

client := http.Client{
Transport: rt,
}
_, err = client.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
}

func TestHostWithNewRoundTripperFromConfig(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != "localhost.localdomain" {
t.Fatalf("Expected Host header in request to be 'localhost.localdomain', got '%s'", r.Host)
}

w.Header().Add("Content-Type", "application/json")
}))
defer ts.Close()

config := DefaultHTTPClientConfig

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

0 comments on commit 0bd6bdd

Please sign in to comment.