2024-06-06 14:42:04 +00:00
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
func Test_getCompressionEncoding(t *testing.T) {
|
2024-06-06 14:42:04 +00:00
|
|
|
testCases := []struct {
|
2024-08-07 14:20:04 +00:00
|
|
|
desc string
|
|
|
|
acceptEncoding []string
|
|
|
|
defaultEncoding string
|
|
|
|
supportedEncodings []string
|
|
|
|
expected string
|
2024-06-06 14:42:04 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "br > gzip (no weight)",
|
|
|
|
acceptEncoding: []string{"gzip, br"},
|
|
|
|
expected: brotliName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
2024-06-12 09:38:04 +00:00
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "zstd > br > gzip (no weight)",
|
|
|
|
acceptEncoding: []string{"zstd, gzip, br"},
|
|
|
|
expected: zstdName,
|
2024-06-12 09:38:04 +00:00
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "known compression encoding (no weight)",
|
|
|
|
acceptEncoding: []string{"compress, gzip"},
|
|
|
|
expected: gzipName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "unknown compression encoding (no weight), no encoding",
|
|
|
|
acceptEncoding: []string{"compress, rar"},
|
|
|
|
expected: identityName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "wildcard return the default compression encoding",
|
|
|
|
acceptEncoding: []string{"*"},
|
|
|
|
expected: brotliName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "wildcard return the custom default compression encoding",
|
|
|
|
acceptEncoding: []string{"*"},
|
|
|
|
defaultEncoding: "foo",
|
|
|
|
expected: "foo",
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "follows weight",
|
|
|
|
acceptEncoding: []string{"br;q=0.8, gzip;q=1.0, *;q=0.1"},
|
|
|
|
expected: gzipName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "ignore unknown compression encoding",
|
|
|
|
acceptEncoding: []string{"compress;q=1.0, gzip;q=0.5"},
|
|
|
|
expected: gzipName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
2024-06-12 09:38:04 +00:00
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "fallback on non-zero compression encoding",
|
|
|
|
acceptEncoding: []string{"compress;q=1.0, gzip, identity;q=0"},
|
|
|
|
expected: gzipName,
|
2024-06-12 09:38:04 +00:00
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "not acceptable (identity)",
|
|
|
|
acceptEncoding: []string{"compress;q=1.0, identity;q=0"},
|
|
|
|
expected: notAcceptable,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "not acceptable (wildcard)",
|
|
|
|
acceptEncoding: []string{"compress;q=1.0, *;q=0"},
|
|
|
|
expected: notAcceptable,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2024-08-07 14:20:04 +00:00
|
|
|
desc: "non-zero is higher than 0",
|
|
|
|
acceptEncoding: []string{"gzip, *;q=0"},
|
|
|
|
expected: gzipName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "zstd forbidden, brotli first",
|
|
|
|
acceptEncoding: []string{"zstd, gzip, br"},
|
|
|
|
supportedEncodings: []string{brotliName, gzipName},
|
|
|
|
expected: brotliName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "follows weight, ignores forbidden encoding",
|
|
|
|
acceptEncoding: []string{"br;q=0.8, gzip;q=1.0, *;q=0.1"},
|
|
|
|
supportedEncodings: []string{zstdName, brotliName},
|
|
|
|
expected: brotliName,
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
2024-10-09 14:28:03 +00:00
|
|
|
{
|
|
|
|
desc: "mixed weight",
|
|
|
|
acceptEncoding: []string{"gzip, br;q=0.9"},
|
|
|
|
supportedEncodings: []string{gzipName, brotliName},
|
|
|
|
expected: gzipName,
|
2024-10-08 09:48:04 +00:00
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
if test.supportedEncodings == nil {
|
|
|
|
test.supportedEncodings = defaultSupportedEncodings
|
|
|
|
}
|
|
|
|
|
|
|
|
encoding := getCompressionEncoding(test.acceptEncoding, test.defaultEncoding, test.supportedEncodings)
|
2024-06-06 14:42:04 +00:00
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
assert.Equal(t, test.expected, encoding)
|
2024-06-06 14:42:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_parseAcceptEncoding(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2024-08-07 14:20:04 +00:00
|
|
|
desc string
|
|
|
|
values []string
|
|
|
|
supportedEncodings []string
|
|
|
|
expected []Encoding
|
|
|
|
assertWeight assert.BoolAssertionFunc
|
2024-06-06 14:42:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "weight",
|
2024-06-12 09:38:04 +00:00
|
|
|
values: []string{"br;q=1.0, zstd;q=0.9, gzip;q=0.8, *;q=0.1"},
|
2024-06-06 14:42:04 +00:00
|
|
|
expected: []Encoding{
|
2024-10-08 09:48:04 +00:00
|
|
|
{Type: brotliName, Weight: 1},
|
|
|
|
{Type: zstdName, Weight: 0.9},
|
|
|
|
{Type: gzipName, Weight: 0.8},
|
|
|
|
{Type: wildcardName, Weight: 0.1},
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-08-07 14:20:04 +00:00
|
|
|
{
|
|
|
|
desc: "weight with supported encodings",
|
|
|
|
values: []string{"br;q=1.0, zstd;q=0.9, gzip;q=0.8, *;q=0.1"},
|
|
|
|
supportedEncodings: []string{brotliName, gzipName},
|
|
|
|
expected: []Encoding{
|
2024-10-09 14:28:03 +00:00
|
|
|
{Type: brotliName, Weight: 1},
|
|
|
|
{Type: gzipName, Weight: 0.8},
|
|
|
|
{Type: wildcardName, Weight: 0.1},
|
2024-08-07 14:20:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
{
|
|
|
|
desc: "mixed",
|
2024-06-12 09:38:04 +00:00
|
|
|
values: []string{"zstd,gzip, br;q=1.0, *;q=0"},
|
2024-06-06 14:42:04 +00:00
|
|
|
expected: []Encoding{
|
2024-10-08 09:48:04 +00:00
|
|
|
{Type: zstdName, Weight: 1},
|
|
|
|
{Type: gzipName, Weight: 1},
|
|
|
|
{Type: brotliName, Weight: 1},
|
|
|
|
{Type: wildcardName, Weight: 0},
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-08-07 14:20:04 +00:00
|
|
|
{
|
|
|
|
desc: "mixed with supported encodings",
|
|
|
|
values: []string{"zstd,gzip, br;q=1.0, *;q=0"},
|
|
|
|
supportedEncodings: []string{zstdName},
|
|
|
|
expected: []Encoding{
|
2024-10-09 14:28:03 +00:00
|
|
|
{Type: zstdName, Weight: 1},
|
|
|
|
{Type: wildcardName, Weight: 0},
|
2024-08-07 14:20:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
{
|
|
|
|
desc: "no weight",
|
2024-06-12 09:38:04 +00:00
|
|
|
values: []string{"zstd, gzip, br, *"},
|
2024-06-06 14:42:04 +00:00
|
|
|
expected: []Encoding{
|
2024-10-08 09:48:04 +00:00
|
|
|
{Type: zstdName, Weight: 1},
|
|
|
|
{Type: gzipName, Weight: 1},
|
|
|
|
{Type: brotliName, Weight: 1},
|
|
|
|
{Type: wildcardName, Weight: 1},
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.False,
|
|
|
|
},
|
2024-08-07 14:20:04 +00:00
|
|
|
{
|
|
|
|
desc: "no weight with supported encodings",
|
|
|
|
values: []string{"zstd, gzip, br, *"},
|
|
|
|
supportedEncodings: []string{"gzip"},
|
|
|
|
expected: []Encoding{
|
2024-10-09 14:28:03 +00:00
|
|
|
{Type: gzipName, Weight: 1},
|
|
|
|
{Type: wildcardName, Weight: 1},
|
2024-08-07 14:20:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.False,
|
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
{
|
|
|
|
desc: "weight and identity",
|
|
|
|
values: []string{"gzip;q=1.0, identity; q=0.5, *;q=0"},
|
|
|
|
expected: []Encoding{
|
2024-10-08 09:48:04 +00:00
|
|
|
{Type: gzipName, Weight: 1},
|
|
|
|
{Type: identityName, Weight: 0.5},
|
|
|
|
{Type: wildcardName, Weight: 0},
|
2024-06-06 14:42:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-08-07 14:20:04 +00:00
|
|
|
{
|
|
|
|
desc: "weight and identity",
|
|
|
|
values: []string{"gzip;q=1.0, identity; q=0.5, *;q=0"},
|
|
|
|
supportedEncodings: []string{"br"},
|
|
|
|
expected: []Encoding{
|
2024-10-09 14:28:03 +00:00
|
|
|
{Type: identityName, Weight: 0.5},
|
|
|
|
{Type: wildcardName, Weight: 0},
|
2024-08-07 14:20:04 +00:00
|
|
|
},
|
|
|
|
assertWeight: assert.True,
|
|
|
|
},
|
2024-06-06 14:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-08-07 14:20:04 +00:00
|
|
|
if test.supportedEncodings == nil {
|
|
|
|
test.supportedEncodings = defaultSupportedEncodings
|
|
|
|
}
|
|
|
|
|
|
|
|
aes, hasWeight := parseAcceptEncoding(test.values, test.supportedEncodings)
|
2024-06-06 14:42:04 +00:00
|
|
|
|
|
|
|
assert.Equal(t, test.expected, aes)
|
|
|
|
test.assertWeight(t, hasWeight)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|