Skip to content
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
6 changes: 5 additions & 1 deletion net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ func makeHTTPRequest(urlStr string, options HTTPRequestOptions, config NetworkCo
}

for name, value := range options.Headers {
req.Header.Set(name, value)
if strings.EqualFold(name, "Host") {
req.Host = value
} else {
req.Header.Set(name, value)
}
result.RequestHeaders.Set(name, value)
}

Expand Down
31 changes: 31 additions & 0 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,37 @@ func TestCheckWebsiteWithHeaders(t *testing.T) {
}
}

func TestCheckWebsiteWithHostHeader(t *testing.T) {
const wantHost = "virtual.example.com"

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != wantHost {
w.WriteHeader(400)
return
}
w.WriteHeader(200)
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()

config := NetworkConfig{
Timeout: 5 * time.Second,
Headers: []string{"Host: " + wantHost},
}

result := CheckWebsite(server.URL, config)

if !result.IsUp {
t.Errorf("CheckWebsite() with Host header should succeed, got status %d", result.StatusCode)
}
if result.StatusCode != 200 {
t.Errorf("CheckWebsite() StatusCode = %d, want 200", result.StatusCode)
}
if got := result.RequestHeaders.Get("Host"); got != wantHost {
t.Errorf("RequestHeaders Host = %q, want %q", got, wantHost)
}
}

func TestCheckWebsiteBodyLimit(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading