Add tests for HTTP content encoding negotiation.

This commit is contained in:
miyuko
2026-06-19 02:02:44 +01:00
parent 223512cd7a
commit 2737f275cf
+39
View File
@@ -0,0 +1,39 @@
package git_pages
import "testing"
func TestHttpContentEncodingNegotiation(t *testing.T) {
check := func(available []string, requested string, expected string) {
parsed := ParseAcceptEncodingHeader(requested)
negotiated := parsed.Negotiate(available...)
if negotiated != expected {
t.Errorf("accept %q, offer %q: expect %q, got %q",
requested, available, expected, negotiated)
}
}
// quality sorting
check([]string{"zstd", "identity"}, "zstd;q=1.0, identity;q=0.5", "zstd")
check([]string{"zstd", "identity"}, "zstd;q=1.0, identity;q=1.0", "zstd")
check([]string{"zstd", "identity"}, "zstd;q=0.5, identity;q=1.0", "identity")
// implicit identity
check([]string{"zstd", "identity"}, "", "identity")
check([]string{"zstd", "identity"}, "zstd;q=0", "identity")
check([]string{"zstd", "identity"}, "compress;q=0", "identity")
// wildcard fallback
check([]string{"zstd", "identity"}, "*", "zstd")
check([]string{"zstd", "identity"}, "identity;q=1, *;q=0", "identity")
check([]string{"zstd", "identity"}, "*;q=0, identity;q=1", "identity")
// negotiation failure
check([]string{"zstd", "identity"}, "*;q=0", "")
check([]string{"zstd", "identity"}, "identity;q=0", "")
check([]string{"zstd", "identity"}, "gzip;q=1.0, *;q=0", "")
// parser lenience
check([]string{"zstd", "identity"}, "zstd;q= 1.0", "identity")
check([]string{"zstd", "identity"}, "zstd;q=1.0000", "identity")
check([]string{"zstd", "identity"}, " zstd ; q=1.0, identity ; q=0.5 , *;q=0 ", "zstd")
}