|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// TestDecompressingTransport verifies that decompressingTransport transparently |
| 14 | +// decompresses Content-Encoding: gzip responses and strips the header. |
| 15 | +// |
| 16 | +// This prevents the "gzip: invalid header" error that occurs when a real |
| 17 | +// http.Transport (used by kubernetes.Clientset) receives a response with |
| 18 | +// Content-Encoding: gzip but a body that has already been decompressed by |
| 19 | +// FilterResp. The transport would wrap the body in a gzip.Reader and fail. |
| 20 | +func TestDecompressingTransport(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + plain := []byte(`{"apiVersion":"v1","kind":"Secret","metadata":{"name":"s","namespace":"default"}}`) |
| 24 | + |
| 25 | + var compressed bytes.Buffer |
| 26 | + gzw := gzip.NewWriter(&compressed) |
| 27 | + _, err := gzw.Write(plain) |
| 28 | + require.NoError(t, err) |
| 29 | + require.NoError(t, gzw.Close()) |
| 30 | + |
| 31 | + t.Run("decompresses gzip and strips header", func(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + base := roundTripFunc(func(_ *http.Request) (*http.Response, error) { |
| 35 | + return &http.Response{ |
| 36 | + StatusCode: http.StatusOK, |
| 37 | + Header: http.Header{"Content-Encoding": []string{"gzip"}, "Content-Type": []string{"application/json"}}, |
| 38 | + Body: io.NopCloser(bytes.NewReader(compressed.Bytes())), |
| 39 | + ContentLength: int64(compressed.Len()), |
| 40 | + }, nil |
| 41 | + }) |
| 42 | + |
| 43 | + resp, err := (&decompressingTransport{base: base}).RoundTrip(&http.Request{}) |
| 44 | + require.NoError(t, err) |
| 45 | + require.Empty(t, resp.Header.Get("Content-Encoding"), |
| 46 | + "Content-Encoding must be stripped after decompression") |
| 47 | + require.Equal(t, int64(len(plain)), resp.ContentLength) |
| 48 | + |
| 49 | + got, err := io.ReadAll(resp.Body) |
| 50 | + require.NoError(t, err) |
| 51 | + require.Equal(t, plain, got) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("non-gzip response passes through unchanged", func(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + base := roundTripFunc(func(_ *http.Request) (*http.Response, error) { |
| 58 | + return &http.Response{ |
| 59 | + StatusCode: http.StatusOK, |
| 60 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 61 | + Body: io.NopCloser(bytes.NewReader(plain)), |
| 62 | + }, nil |
| 63 | + }) |
| 64 | + |
| 65 | + resp, err := (&decompressingTransport{base: base}).RoundTrip(&http.Request{}) |
| 66 | + require.NoError(t, err) |
| 67 | + require.Empty(t, resp.Header.Get("Content-Encoding")) |
| 68 | + |
| 69 | + got, err := io.ReadAll(resp.Body) |
| 70 | + require.NoError(t, err) |
| 71 | + require.Equal(t, plain, got) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +// roundTripFunc is a helper that implements http.RoundTripper via a function. |
| 76 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 77 | + |
| 78 | +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 79 | + return f(req) |
| 80 | +} |
0 commit comments