HTTP Prove: Removes Accept-Encoding header from http probe

The Accept-Encoding header is added by default because compression is enabled by default. By removing it we are reducing the sent payload.
This commit is contained in:
Hugo Fonseca 2020-11-09 22:20:56 +00:00
parent d9d626f1e6
commit ba514718da
2 changed files with 29 additions and 7 deletions

View File

@ -70,6 +70,7 @@ type httpProber struct {
// Probe returns a ProbeRunner capable of running an HTTP check.
func (pr httpProber) Probe(url *url.URL, headers http.Header, timeout time.Duration) (probe.Result, string, error) {
pr.transport.DisableCompression = true // removes Accept-Encoding header
client := &http.Client{
Timeout: timeout,
Transport: pr.transport,
@ -104,15 +105,12 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr
if _, ok := headers["Accept"]; !ok {
// Accept header was not defined. accept all
headers.Set("Accept", "*/*")
}
if headers.Get("Accept") == "" {
} else 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")
}
req.Host = headers.Get("Host")
res, err := client.Do(req)
if err != nil {
// Convert errors into failures to catch timeouts.

View File

@ -158,13 +158,37 @@ func TestHTTPProbeChecker(t *testing.T) {
handler: headerCounterHandler,
reqHeaders: http.Header{},
health: probe.Success,
accBody: "4",
accBody: "3",
},
{
handler: headerKeysNamesHandler,
reqHeaders: http.Header{},
health: probe.Success,
accBody: "Accept\nAccept-Encoding\nConnection\nUser-Agent",
accBody: "Accept\nConnection\nUser-Agent",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"Accept-Encoding": {"gzip"},
},
health: probe.Success,
accBody: "Accept-Encoding: gzip",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"Accept-Encoding": {"foo"},
},
health: probe.Success,
accBody: "Accept-Encoding: foo",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"Accept-Encoding": {""},
},
health: probe.Success,
accBody: "Accept-Encoding: \n",
},
{
handler: headerEchoHandler,