From cdac627c8b006ea89bbb451eb3ee14737d7645cc Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Mon, 23 Dec 2024 18:09:00 +0100 Subject: [PATCH] Add accept encoding test --- proxy_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/proxy_test.go b/proxy_test.go index 2cafa9b2..c7b0d505 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "regexp" + "strconv" "strings" "testing" "time" @@ -481,6 +482,54 @@ func TestNoProxyHeadersHttps(t *testing.T) { _, _ = client.Do(req) } +type VerifyAcceptEncodingHeader struct { + ReceivedHeaderValue string +} + +func (v *VerifyAcceptEncodingHeader) ServeHTTP(w http.ResponseWriter, r *http.Request) { + v.ReceivedHeaderValue = r.Header.Get("Accept-Encoding") +} + +func TestAcceptEncoding(t *testing.T) { + v := VerifyAcceptEncodingHeader{} + s := httptest.NewServer(&v) + for i, tc := range []struct { + keepAcceptEncoding bool + disableCompression bool + acceptEncoding string + expectedValue string + }{ + {false, false, "", "gzip"}, + {false, false, "identity", "gzip"}, + {false, true, "", ""}, + {false, true, "identity", ""}, + {true, false, "", "gzip"}, + {true, false, "identity", "identity"}, + {true, true, "", ""}, + {true, true, "identity", "identity"}, + } { + t.Run(strconv.Itoa(i), func(t *testing.T) { + proxy := goproxy.NewProxyHttpServer() + proxy.KeepAcceptEncoding = tc.keepAcceptEncoding + proxy.Tr.DisableCompression = tc.disableCompression + client, l := oneShotProxy(proxy) + defer l.Close() + req, err := http.NewRequest("GET", s.URL, nil) + panicOnErr(err, "bad request") + // fully control the Accept-Encoding header we send to the proxy + client.Transport.(*http.Transport).DisableCompression = true + if tc.acceptEncoding != "" { + req.Header.Add("Accept-Encoding", tc.acceptEncoding) + } + _, err = client.Do(req) + panicOnErr(err, "bad response") + if v.ReceivedHeaderValue != tc.expectedValue { + t.Errorf("%+v expected Accept-Encoding: %s, got %s", tc, tc.expectedValue, v.ReceivedHeaderValue) + } + }) + } +} + func TestHeadReqHasContentLength(t *testing.T) { client, l := oneShotProxy(goproxy.NewProxyHttpServer()) defer l.Close()