| 1 | package compresshttp_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/sassoftware/relic/v7/lib/compresshttp" |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | "github.com/stretchr/testify/require" |
| 15 | ) |
| 16 | |
| 17 | func TestCompress(t *testing.T) { |
| 18 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | if r.URL.Path == "/fail" { |
| 20 | http.NotFound(w, r) |
| 21 | return |
| 22 | } |
| 23 | rbody, err := io.ReadAll(r.Body) |
| 24 | if assert.NoError(t, err) { |
| 25 | assert.Len(t, rbody, 8192) |
| 26 | } |
| 27 | // write 16384 bytes in two chunks with a flush in between |
| 28 | d := make([]byte, 8192) |
| 29 | _, err = w.Write(d) |
| 30 | require.NoError(t, err) |
| 31 | w.(http.Flusher).Flush() |
| 32 | _, err = w.Write(d) |
| 33 | require.NoError(t, err) |
| 34 | }) |
| 35 | srv := httptest.NewServer(compresshttp.Middleware(h)) |
| 36 | defer srv.Close() |
| 37 | for _, ae := range []string{"", "identity", "gzip", "x-snappy-framed"} { |
| 38 | t.Run("Accept_"+ae, func(t *testing.T) { |
| 39 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 40 | defer cancel() |
| 41 | d := make([]byte, 8192) |
| 42 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL, bytes.NewReader(d)) |
| 43 | require.NoError(t, err) |
| 44 | require.NoError(t, compresshttp.CompressRequest(req, ae)) |
| 45 | if ae != "" { |
| 46 | req.Header.Set("Accept-Encoding", ae) |
| 47 | } |
| 48 | resp, err := http.DefaultClient.Do(req) |
| 49 | require.NoError(t, err) |
| 50 | assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 51 | if ae == "identity" { |
| 52 | assert.Empty(t, resp.Header.Get("Content-Encoding")) |
| 53 | } else { |
| 54 | assert.Equal(t, ae, resp.Header.Get("Content-Encoding")) |
| 55 | } |
| 56 | require.NoError(t, compresshttp.DecompressResponse(resp)) |
| 57 | body, err := io.ReadAll(resp.Body) |
| 58 | require.NoError(t, err) |
| 59 | assert.Len(t, body, 16384) |
| 60 | assert.Equal(t, compresshttp.AcceptedEncodings, resp.Header.Get("Accept-Encoding")) |
| 61 | }) |
| 62 | } |
| 63 | t.Run("Error", func(t *testing.T) { |
| 64 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 65 | defer cancel() |
| 66 | d := make([]byte, 8192) |
| 67 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+"/fail", bytes.NewReader(d)) |
| 68 | require.NoError(t, err) |
| 69 | require.NoError(t, compresshttp.CompressRequest(req, compresshttp.EncodingSnappy)) |
| 70 | req.Header.Set("Accept-Encoding", compresshttp.AcceptedEncodings) |
| 71 | resp, err := http.DefaultClient.Do(req) |
| 72 | require.NoError(t, err) |
| 73 | assert.Equal(t, http.StatusNotFound, resp.StatusCode) |
| 74 | assert.Empty(t, resp.Header.Get("Content-Encoding")) |
| 75 | require.NoError(t, compresshttp.DecompressResponse(resp)) |
| 76 | body, err := io.ReadAll(resp.Body) |
| 77 | require.NoError(t, err) |
| 78 | assert.Contains(t, string(body), "not found") |
| 79 | }) |
| 80 | t.Run("Unsupported", func(t *testing.T) { |
| 81 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 82 | defer cancel() |
| 83 | d := make([]byte, 8192) |
| 84 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL, bytes.NewReader(d)) |
| 85 | require.NoError(t, err) |
| 86 | req.Header.Set("Content-Encoding", "spam") |
| 87 | resp, err := http.DefaultClient.Do(req) |
| 88 | require.NoError(t, err) |
| 89 | assert.Equal(t, http.StatusUnsupportedMediaType, resp.StatusCode) |
| 90 | assert.Equal(t, compresshttp.AcceptedEncodings, resp.Header.Get("Accept-Encoding")) |
| 91 | }) |
| 92 | } |