(t *testing.T)
| 19 | ) |
| 20 | |
| 21 | func TestCompressorEncodings(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | path string |
| 27 | expectedEncoding string |
| 28 | acceptedEncodings []string |
| 29 | }{ |
| 30 | { |
| 31 | name: "no expected encodings due to no accepted encodings", |
| 32 | path: "/file.html", |
| 33 | acceptedEncodings: nil, |
| 34 | expectedEncoding: "", |
| 35 | }, |
| 36 | { |
| 37 | name: "gzip is only encoding", |
| 38 | path: "/file.html", |
| 39 | acceptedEncodings: []string{"gzip"}, |
| 40 | expectedEncoding: "gzip", |
| 41 | }, |
| 42 | { |
| 43 | name: "gzip is preferred over deflate", |
| 44 | path: "/file.html", |
| 45 | acceptedEncodings: []string{"gzip", "deflate"}, |
| 46 | expectedEncoding: "gzip", |
| 47 | }, |
| 48 | { |
| 49 | name: "deflate is used", |
| 50 | path: "/file.html", |
| 51 | acceptedEncodings: []string{"deflate"}, |
| 52 | expectedEncoding: "deflate", |
| 53 | }, |
| 54 | { |
| 55 | name: "nop is preferred", |
| 56 | path: "/file.html", |
| 57 | acceptedEncodings: []string{"nop, gzip, deflate"}, |
| 58 | expectedEncoding: "nop", |
| 59 | }, |
| 60 | } |
| 61 | |
| 62 | for _, tc := range tests { |
| 63 | t.Run(tc.name, func(t *testing.T) { |
| 64 | t.Parallel() |
| 65 | logger := testutil.Logger(t) |
| 66 | tempDir := t.TempDir() |
| 67 | cacheDir := filepath.Join(tempDir, "cache") |
| 68 | err := os.MkdirAll(cacheDir, 0o700) |
| 69 | require.NoError(t, err) |
| 70 | srcDir := filepath.Join(tempDir, "src") |
| 71 | err = os.MkdirAll(srcDir, 0o700) |
| 72 | require.NoError(t, err) |
| 73 | err = os.WriteFile(filepath.Join(srcDir, "file.html"), []byte("textstring"), 0o600) |
| 74 | require.NoError(t, err) |
| 75 | |
| 76 | compressor := NewCompressor(logger, 5, cacheDir, http.FS(os.DirFS(srcDir))) |
| 77 | if len(compressor.encoders) != 0 || len(compressor.pooledEncoders) != 2 { |
| 78 | t.Errorf("gzip and deflate should be pooled") |
nothing calls this directly
no test coverage detected