Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set host header if provided #304

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading