Debian Codegraph @

internal/github.com/golang/gddo/httputil/negotiate_test.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd.
6
7 package httputil
8
9 import (
10 "net/http"
11 "testing"
12 )
13
14 var negotiateContentEncodingTests = []struct {
15 s string
16 offers []string
17 expect string
18 }{
19 {"", []string{"identity", "gzip"}, "identity"},
20 {"*;q=0", []string{"identity", "gzip"}, ""},
21 {"gzip", []string{"identity", "gzip"}, "gzip"},
22 {"gzip,zstd", []string{"identity", "zstd"}, "zstd"},
23 {"zstd,gzip", []string{"gzip", "zstd"}, "gzip"},
24 {"gzip,zstd", []string{"gzip", "zstd"}, "gzip"},
25 {"gzip,zstd", []string{"zstd", "gzip"}, "zstd"},
26 {"gzip;q=0.1,zstd;q=0.5", []string{"gzip", "zstd"}, "zstd"},
27 {"gzip;q=1.0, identity; q=0.5, *;q=0", []string{"identity", "gzip"}, "gzip"},
28 {"gzip;q=1.0, identity; q=0.5, *;q=0", []string{"identity", "zstd"}, "identity"},
29 {"zstd", []string{"identity", "gzip"}, "identity"},
30 }
31
32 func TestNegotiateContentEncoding(t *testing.T) {
33 for _, tt := range negotiateContentEncodingTests {
34 r := &http.Request{Header: http.Header{"Accept-Encoding": {tt.s}}}
35 actual := NegotiateContentEncoding(r, tt.offers)
36 if actual != tt.expect {
37 t.Errorf("NegotiateContentEncoding(%q, %#v)=%q, want %q", tt.s, tt.offers, actual, tt.expect)
38 }
39 }
40 }