|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/labstack/echo/v4" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestDecompress(t *testing.T) { |
| 17 | + e := echo.New() |
| 18 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("test")) |
| 19 | + rec := httptest.NewRecorder() |
| 20 | + c := e.NewContext(req, rec) |
| 21 | + |
| 22 | + // Skip if no Content-Encoding header |
| 23 | + h := Decompress()(func(c echo.Context) error { |
| 24 | + c.Response().Write([]byte("test")) // For Content-Type sniffing |
| 25 | + return nil |
| 26 | + }) |
| 27 | + h(c) |
| 28 | + |
| 29 | + assert := assert.New(t) |
| 30 | + assert.Equal("test", rec.Body.String()) |
| 31 | + |
| 32 | + // Decompress |
| 33 | + body := `{"name": "echo"}` |
| 34 | + gz, _ := gzipString(body) |
| 35 | + req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(gz))) |
| 36 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 37 | + rec = httptest.NewRecorder() |
| 38 | + c = e.NewContext(req, rec) |
| 39 | + h(c) |
| 40 | + assert.Equal(GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding)) |
| 41 | + b, err := ioutil.ReadAll(req.Body) |
| 42 | + assert.NoError(err) |
| 43 | + assert.Equal(body, string(b)) |
| 44 | +} |
| 45 | + |
| 46 | +func TestCompressRequestWithoutDecompressMiddleware(t *testing.T) { |
| 47 | + e := echo.New() |
| 48 | + body := `{"name":"echo"}` |
| 49 | + gz, _ := gzipString(body) |
| 50 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(gz))) |
| 51 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + e.NewContext(req, rec) |
| 54 | + e.ServeHTTP(rec, req) |
| 55 | + assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding)) |
| 56 | + b, err := ioutil.ReadAll(req.Body) |
| 57 | + assert.NoError(t, err) |
| 58 | + assert.NotEqual(t, b, body) |
| 59 | + assert.Equal(t, b, gz) |
| 60 | +} |
| 61 | + |
| 62 | +func TestDecompressNoContent(t *testing.T) { |
| 63 | + e := echo.New() |
| 64 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 65 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 66 | + rec := httptest.NewRecorder() |
| 67 | + c := e.NewContext(req, rec) |
| 68 | + h := Decompress()(func(c echo.Context) error { |
| 69 | + return c.NoContent(http.StatusNoContent) |
| 70 | + }) |
| 71 | + if assert.NoError(t, h(c)) { |
| 72 | + assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding)) |
| 73 | + assert.Empty(t, rec.Header().Get(echo.HeaderContentType)) |
| 74 | + assert.Equal(t, 0, len(rec.Body.Bytes())) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestDecompressErrorReturned(t *testing.T) { |
| 79 | + e := echo.New() |
| 80 | + e.Use(Decompress()) |
| 81 | + e.GET("/", func(c echo.Context) error { |
| 82 | + return echo.ErrNotFound |
| 83 | + }) |
| 84 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 85 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 86 | + rec := httptest.NewRecorder() |
| 87 | + e.ServeHTTP(rec, req) |
| 88 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 89 | + assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding)) |
| 90 | +} |
| 91 | + |
| 92 | +func TestDecompressSkipper(t *testing.T) { |
| 93 | + e := echo.New() |
| 94 | + e.Use(DecompressWithConfig(DecompressConfig{ |
| 95 | + Skipper: func(c echo.Context) bool { |
| 96 | + return c.Request().URL.Path == "/skip" |
| 97 | + }, |
| 98 | + })) |
| 99 | + body := `{"name": "echo"}` |
| 100 | + req := httptest.NewRequest(http.MethodPost, "/skip", strings.NewReader(body)) |
| 101 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 102 | + rec := httptest.NewRecorder() |
| 103 | + c := e.NewContext(req, rec) |
| 104 | + e.ServeHTTP(rec, req) |
| 105 | + assert.Equal(t, rec.Header().Get(echo.HeaderContentType), echo.MIMEApplicationJSONCharsetUTF8) |
| 106 | + reqBody, err := ioutil.ReadAll(c.Request().Body) |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.Equal(t, body, string(reqBody)) |
| 109 | +} |
| 110 | + |
| 111 | +func BenchmarkDecompress(b *testing.B) { |
| 112 | + e := echo.New() |
| 113 | + body := `{"name": "echo"}` |
| 114 | + gz, _ := gzipString(body) |
| 115 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(gz))) |
| 116 | + req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding) |
| 117 | + |
| 118 | + h := Decompress()(func(c echo.Context) error { |
| 119 | + c.Response().Write([]byte(body)) // For Content-Type sniffing |
| 120 | + return nil |
| 121 | + }) |
| 122 | + |
| 123 | + b.ReportAllocs() |
| 124 | + b.ResetTimer() |
| 125 | + |
| 126 | + for i := 0; i < b.N; i++ { |
| 127 | + // Decompress |
| 128 | + rec := httptest.NewRecorder() |
| 129 | + c := e.NewContext(req, rec) |
| 130 | + h(c) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func gzipString(body string) ([]byte, error) { |
| 135 | + var buf bytes.Buffer |
| 136 | + gz := gzip.NewWriter(&buf) |
| 137 | + |
| 138 | + _, err := gz.Write([]byte(body)) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + |
| 143 | + if err := gz.Close(); err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + return buf.Bytes(), nil |
| 148 | +} |
0 commit comments