gzip_handler_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package util
  2. import (
  3. "compress/gzip"
  4. "github.com/stretchr/testify/require"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. func TestGzipHandler(t *testing.T) {
  11. s := Gzip(http.FileServer(http.FS(testFs)))
  12. rr := httptest.NewRecorder()
  13. req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
  14. req.Header.Set("Accept-Encoding", "gzip, deflate")
  15. s.ServeHTTP(rr, req)
  16. require.Equal(t, 200, rr.Code)
  17. require.Equal(t, "gzip", rr.Header().Get("Content-Encoding"))
  18. require.Equal(t, "", rr.Header().Get("Content-Length"))
  19. gz, _ := gzip.NewReader(rr.Body)
  20. b, _ := io.ReadAll(gz)
  21. require.Equal(t, "This is a test file for embedfs_test.go\n", string(b))
  22. }
  23. func TestGzipHandler_NoGzip(t *testing.T) {
  24. s := Gzip(http.FileServer(http.FS(testFs)))
  25. rr := httptest.NewRecorder()
  26. req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
  27. s.ServeHTTP(rr, req)
  28. require.Equal(t, 200, rr.Code)
  29. require.Equal(t, "", rr.Header().Get("Content-Encoding"))
  30. require.Equal(t, "40", rr.Header().Get("Content-Length"))
  31. b, _ := io.ReadAll(rr.Body)
  32. require.Equal(t, "This is a test file for embedfs_test.go\n", string(b))
  33. }