Set Host header in HTTP provider request

This commit is contained in:
Nikolai K 2024-10-29 15:30:38 +01:00 committed by GitHub
parent 7004f0e750
commit e8ff825ed2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"hash/fnv"
"io"
"net/http"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
@ -149,8 +150,12 @@ func (p *Provider) fetchConfigurationData() ([]byte, error) {
}
for k, v := range p.Headers {
if strings.EqualFold(k, "Host") {
req.Host = v
} else {
req.Header.Set(k, v)
}
}
res, err := p.httpClient.Do(req)
if err != nil {

View file

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
@ -87,6 +88,7 @@ func TestProvider_fetchConfigurationData(t *testing.T) {
headers: map[string]string{
"Foo": "bar",
"Bar": "baz",
"Host": "localhost",
},
expData: []byte("{}"),
expErr: require.NoError,
@ -105,8 +107,12 @@ func TestProvider_fetchConfigurationData(t *testing.T) {
handlerCalled = true
for k, v := range test.headers {
if strings.EqualFold(k, "Host") {
assert.Equal(t, v, req.Host)
} else {
assert.Equal(t, v, req.Header.Get(k))
}
}
rw.WriteHeader(test.statusCode)
_, _ = rw.Write([]byte("{}"))