From 0794bf65bcde0e42fc3e5178f640bc12aec12ea0 Mon Sep 17 00:00:00 2001 From: Hugo Fonseca <1098293+fonsecas72@users.noreply.github.com> Date: Fri, 30 Oct 2020 00:32:49 +0000 Subject: [PATCH] HTTP Probe: Add 'Accept' header by default When using a HTTP probe, the request will now have a "Accept" header by default with the "*/*" (accept all) Most tools do add this header (see curl) so it's a reasonable expectation that http probe has it as well --- pkg/probe/http/http.go | 8 ++++ pkg/probe/http/http_test.go | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/pkg/probe/http/http.go b/pkg/probe/http/http.go index 02cdcc045c8..c11e03f18ae 100644 --- a/pkg/probe/http/http.go +++ b/pkg/probe/http/http.go @@ -101,6 +101,14 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr v := version.Get() headers.Set("User-Agent", fmt.Sprintf("kube-probe/%s.%s", v.Major, v.Minor)) } + if _, ok := headers["Accept"]; !ok { + // Accept header was not defined. accept all + headers.Set("Accept", "*/*") + } + if headers.Get("Accept") == "" { + // Accept header was overridden but is empty. removing + headers.Del("Accept") + } req.Header = headers if headers.Get("Host") != "" { req.Host = headers.Get("Host") diff --git a/pkg/probe/http/http_test.go b/pkg/probe/http/http_test.go index 16b5b1b0208..f02dee7c123 100644 --- a/pkg/probe/http/http_test.go +++ b/pkg/probe/http/http_test.go @@ -24,6 +24,7 @@ import ( "net/http/httptest" "net/url" "os" + "sort" "strconv" "strings" "testing" @@ -110,6 +111,24 @@ func TestHTTPProbeChecker(t *testing.T) { w.Write([]byte(output)) } + // Handler that returns the number of request headers in the body + headerCounterHandler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + w.Write([]byte(strconv.Itoa(len(r.Header)))) + } + + // Handler that returns the keys of request headers in the body + headerKeysNamesHandler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + keys := make([]string, 0, len(r.Header)) + for k := range r.Header { + keys = append(keys, k) + } + sort.Strings(keys) + + w.Write([]byte(strings.Join(keys, "\n"))) + } + redirectHandler := func(s int, bad bool) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { @@ -135,6 +154,18 @@ func TestHTTPProbeChecker(t *testing.T) { health: probe.Success, accBody: "ok body", }, + { + handler: headerCounterHandler, + reqHeaders: http.Header{}, + health: probe.Success, + accBody: "4", + }, + { + handler: headerKeysNamesHandler, + reqHeaders: http.Header{}, + health: probe.Success, + accBody: "Accept\nAccept-Encoding\nConnection\nUser-Agent", + }, { handler: headerEchoHandler, reqHeaders: http.Header{ @@ -165,6 +196,64 @@ func TestHTTPProbeChecker(t *testing.T) { health: probe.Success, accBody: "User-Agent: kube-probe/", }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "User-Agent": {"foo/1.0"}, + "Accept": {"text/html"}, + }, + health: probe.Success, + accBody: "Accept: text/html", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "User-Agent": {"foo/1.0"}, + "Accept": {"foo/*"}, + }, + health: probe.Success, + accBody: "User-Agent: foo/1.0", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "X-Muffins-Or-Cupcakes": {"muffins"}, + "Accept": {"foo/*"}, + }, + health: probe.Success, + accBody: "X-Muffins-Or-Cupcakes: muffins", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "Accept": {"foo/*"}, + }, + health: probe.Success, + accBody: "Accept: foo/*", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "Accept": {""}, + }, + health: probe.Success, + notBody: "Accept:", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{ + "User-Agent": {"foo/1.0"}, + "Accept": {""}, + }, + health: probe.Success, + notBody: "Accept:", + }, + { + handler: headerEchoHandler, + reqHeaders: http.Header{}, + health: probe.Success, + accBody: "Accept: */*", + }, { // Echo handler that returns the contents of Host in the body handler: func(w http.ResponseWriter, r *http.Request) {