From e8ff825ed272669604fc6c9ad2475f5019992f07 Mon Sep 17 00:00:00 2001 From: Nikolai K <45662151+nikonhub@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:30:38 +0100 Subject: [PATCH] Set Host header in HTTP provider request --- pkg/provider/http/http.go | 7 ++++++- pkg/provider/http/http_test.go | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/provider/http/http.go b/pkg/provider/http/http.go index 4da8a2973..1068429b2 100644 --- a/pkg/provider/http/http.go +++ b/pkg/provider/http/http.go @@ -7,6 +7,7 @@ import ( "hash/fnv" "io" "net/http" + "strings" "time" "github.com/cenkalti/backoff/v4" @@ -149,7 +150,11 @@ func (p *Provider) fetchConfigurationData() ([]byte, error) { } for k, v := range p.Headers { - req.Header.Set(k, v) + if strings.EqualFold(k, "Host") { + req.Host = v + } else { + req.Header.Set(k, v) + } } res, err := p.httpClient.Do(req) diff --git a/pkg/provider/http/http_test.go b/pkg/provider/http/http_test.go index f99706ba9..c9f932536 100644 --- a/pkg/provider/http/http_test.go +++ b/pkg/provider/http/http_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -85,8 +86,9 @@ func TestProvider_fetchConfigurationData(t *testing.T) { desc: "should send configured headers", statusCode: http.StatusOK, headers: map[string]string{ - "Foo": "bar", - "Bar": "baz", + "Foo": "bar", + "Bar": "baz", + "Host": "localhost", }, expData: []byte("{}"), expErr: require.NoError, @@ -105,7 +107,11 @@ func TestProvider_fetchConfigurationData(t *testing.T) { handlerCalled = true for k, v := range test.headers { - assert.Equal(t, v, req.Header.Get(k)) + if strings.EqualFold(k, "Host") { + assert.Equal(t, v, req.Host) + } else { + assert.Equal(t, v, req.Header.Get(k)) + } } rw.WriteHeader(test.statusCode)