Skip to content

Commit

Permalink
set host header if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed Aug 30, 2024
1 parent c57f0f0 commit eb0c994
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/clients/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/clients/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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)
}

0 comments on commit eb0c994

Please sign in to comment.