1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package middleware
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
)
func TestContentEncodingMiddleware(t *testing.T) {
t.Parallel()
// support for:
// Content-Encoding: gzip
// Content-Encoding: deflate
// Content-Encoding: gzip, deflate
// Content-Encoding: deflate, gzip
middleware := AllowContentEncoding("deflate", "gzip")
tests := []struct {
name string
encodings []string
expectedStatus int
}{
{
name: "Support no encoding",
encodings: []string{},
expectedStatus: 200,
},
{
name: "Support gzip encoding",
encodings: []string{"gzip"},
expectedStatus: 200,
},
{
name: "No support for br encoding",
encodings: []string{"br"},
expectedStatus: 415,
},
{
name: "Support for gzip and deflate encoding",
encodings: []string{"gzip", "deflate"},
expectedStatus: 200,
},
{
name: "Support for deflate and gzip encoding",
encodings: []string{"deflate", "gzip"},
expectedStatus: 200,
},
{
name: "No support for deflate and br encoding",
encodings: []string{"deflate", "br"},
expectedStatus: 415,
},
}
for _, tt := range tests {
var tt = tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
body := []byte("This is my content. There are many like this but this one is mine")
r := httptest.NewRequest("POST", "/", bytes.NewReader(body))
for _, encoding := range tt.encodings {
r.Header.Set("Content-Encoding", encoding)
}
w := httptest.NewRecorder()
router := chi.NewRouter()
router.Use(middleware)
router.Post("/", func(w http.ResponseWriter, r *http.Request) {})
router.ServeHTTP(w, r)
res := w.Result()
if res.StatusCode != tt.expectedStatus {
t.Errorf("response is incorrect, got %d, want %d", w.Code, tt.expectedStatus)
}
})
}
}
|