ntfy/util/gzip_handler_test.go
Nick Farrell b218e62ffc
Actually apply the pre-commit fixers to the codebase.
This can be redone manually with
`pre-commit run --all`

While the pre-commit hook could be merged to run locally,
it is much cleaner to align all the files to best-practice
syntax in a single commit. It is also required for server-side
validation.
2022-12-23 08:15:51 +11:00

41 lines
1.1 KiB
Go

package util
import (
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestGzipHandler(t *testing.T) {
s := Gzip(http.FileServer(http.FS(testFs)))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
req.Header.Set("Accept-Encoding", "gzip, deflate")
s.ServeHTTP(rr, req)
require.Equal(t, 200, rr.Code)
require.Equal(t, "gzip", rr.Header().Get("Content-Encoding"))
require.Equal(t, "", rr.Header().Get("Content-Length"))
gz, _ := gzip.NewReader(rr.Body)
b, _ := io.ReadAll(gz)
require.Equal(t, "This is a test file for embedfs_test.go\n", string(b))
}
func TestGzipHandler_NoGzip(t *testing.T) {
s := Gzip(http.FileServer(http.FS(testFs)))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
s.ServeHTTP(rr, req)
require.Equal(t, 200, rr.Code)
require.Equal(t, "", rr.Header().Get("Content-Encoding"))
require.Equal(t, "40", rr.Header().Get("Content-Length"))
b, _ := io.ReadAll(rr.Body)
require.Equal(t, "This is a test file for embedfs_test.go\n", string(b))
}