Skip to content

Commit

Permalink
Add accept encoding test
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikPelli committed Dec 23, 2024
1 parent 6dec8d5 commit cdac627
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)

Check failure on line 517 in proxy_test.go

View workflow job for this annotation

GitHub Actions / Build And Test Go code

should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
panicOnErr(err, "bad request")
// fully control the Accept-Encoding header we send to the proxy
client.Transport.(*http.Transport).DisableCompression = true

Check failure on line 520 in proxy_test.go

View workflow job for this annotation

GitHub Actions / Build And Test Go code

type assertion must be checked (forcetypeassert)
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()
Expand Down

0 comments on commit cdac627

Please sign in to comment.