From eb0c994eeafa7169905a8930115ab980540eafb4 Mon Sep 17 00:00:00 2001 From: Erik Veld Date: Fri, 30 Aug 2024 16:19:46 +0200 Subject: [PATCH] set host header if provided --- pkg/clients/http/http.go | 5 +++++ pkg/clients/http/http_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/pkg/clients/http/http.go b/pkg/clients/http/http.go index c709967f..b8f9ea45 100644 --- a/pkg/clients/http/http.go +++ b/pkg/clients/http/http.go @@ -68,6 +68,11 @@ func (h *HTTPImpl) HealthCheckHTTP(address, method string, headers map[string][] rq.Header = headers + hosts, ok := headers["Host"] + if ok && len(hosts) > 0 { + rq.Host = hosts[0] + } + if len(codes) == 0 { codes = []int{200} } diff --git a/pkg/clients/http/http_test.go b/pkg/clients/http/http_test.go index be25c2a6..76e97630 100644 --- a/pkg/clients/http/http_test.go +++ b/pkg/clients/http/http_test.go @@ -3,6 +3,7 @@ package http import ( "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -66,3 +67,41 @@ func TestHTTPHealthErrorsOnClientError(t *testing.T) { assert.Error(t, err) assert.Len(t, *reqs, 0) } + +func TestHTTPHealthSetsHostOnHostHeader(t *testing.T) { + url, reqs, cleanup := testSetupHTTPBasicServer(http.StatusOK, "") + defer cleanup() + + c := NewHTTP(1*time.Millisecond, logger.NewTestLogger(t)) + + err := c.HealthCheckHTTP(url, "", map[string][]string{"Host": {"example.com"}}, "", []int{200}, 10*time.Millisecond) + assert.NoError(t, err) + assert.Len(t, *reqs, 1) + assert.Equal(t, "example.com", (*reqs)[0].Host) +} + +func TestHTTPHealthSetsHostOnHostHeaderWithMultipleValues(t *testing.T) { + url, reqs, cleanup := testSetupHTTPBasicServer(http.StatusOK, "") + defer cleanup() + + c := NewHTTP(1*time.Millisecond, logger.NewTestLogger(t)) + + err := c.HealthCheckHTTP(url, "", map[string][]string{"Host": {"example.com", "example.org"}}, "", []int{200}, 10*time.Millisecond) + assert.NoError(t, err) + assert.Len(t, *reqs, 1) + assert.Equal(t, "example.com", (*reqs)[0].Host) +} + +func TestHTTPHealthSetsHostOnHostHeaderWithNoValues(t *testing.T) { + url, reqs, cleanup := testSetupHTTPBasicServer(http.StatusOK, "") + defer cleanup() + + host := strings.TrimPrefix(url, "http://") + + c := NewHTTP(1*time.Millisecond, logger.NewTestLogger(t)) + + err := c.HealthCheckHTTP(url, "", map[string][]string{}, "", []int{200}, 10*time.Millisecond) + assert.NoError(t, err) + assert.Len(t, *reqs, 1) + assert.Equal(t, host, (*reqs)[0].Host) +}