diff --git a/api/api.go b/api/api.go index b90a45d92b..83784284b5 100644 --- a/api/api.go +++ b/api/api.go @@ -368,6 +368,9 @@ type Config struct { // HttpAuth is the auth info to use for http access. HttpAuth *HttpBasicAuth + // HostOverride is the override to host. Default is Address. + HostOverride string + // WaitTime limits how long a Watch will block. If not provided, // the agent default values will be used. WaitTime time.Duration @@ -1029,6 +1032,9 @@ func (r *request) toHTTP() (*http.Request, error) { req.URL.Host = r.url.Host req.URL.Scheme = r.url.Scheme req.Host = r.url.Host + if r.config.HostOverride != "" { + req.Host = r.config.HostOverride + } req.Header = r.header // Content-Type must always be set when a body is present diff --git a/api/api_test.go b/api/api_test.go index e8a03f7218..68d24130a7 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -994,6 +994,22 @@ func TestAPI_RequestToHTTP(t *testing.T) { } } +func TestAPI_RequestToHTTP_HostOverride(t *testing.T) { + t.Parallel() + c, s := makeClient(t) + defer s.Stop() + + r := c.newRequest("DELETE", "/v1/kv/foo") + q := &QueryOptions{ + Datacenter: "foo", + } + r.setQueryOptions(q) + r.config.HostOverride = "https.consul.unix.socket" + req, err := r.toHTTP() + require.NoError(t, err) + require.Equal(t, "https.consul.unix.socket", req.Host) +} + func TestAPI_ParseQueryMeta(t *testing.T) { t.Parallel() resp := &http.Response{