aboutsummaryrefslogtreecommitdiff
path: root/httputil/negotiate_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'httputil/negotiate_test.go')
-rw-r--r--httputil/negotiate_test.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/httputil/negotiate_test.go b/httputil/negotiate_test.go
new file mode 100644
index 0000000..24bf4be
--- /dev/null
+++ b/httputil/negotiate_test.go
@@ -0,0 +1,71 @@
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
7package httputil_test
8
9import (
10 "github.com/golang/gddo/httputil"
11 "net/http"
12 "testing"
13)
14
15var negotiateContentEncodingTests = []struct {
16 s string
17 offers []string
18 expect string
19}{
20 {"", []string{"identity", "gzip"}, "identity"},
21 {"*;q=0", []string{"identity", "gzip"}, ""},
22 {"gzip", []string{"identity", "gzip"}, "gzip"},
23}
24
25func TestNegotiateContentEnoding(t *testing.T) {
26 for _, tt := range negotiateContentEncodingTests {
27 r := &http.Request{Header: http.Header{"Accept-Encoding": {tt.s}}}
28 actual := httputil.NegotiateContentEncoding(r, tt.offers)
29 if actual != tt.expect {
30 t.Errorf("NegotiateContentEncoding(%q, %#v)=%q, want %q", tt.s, tt.offers, actual, tt.expect)
31 }
32 }
33}
34
35var negotiateContentTypeTests = []struct {
36 s string
37 offers []string
38 defaultOffer string
39 expect string
40}{
41 {"text/html, */*;q=0", []string{"x/y"}, "", ""},
42 {"text/html, */*", []string{"x/y"}, "", "x/y"},
43 {"text/html, image/png", []string{"text/html", "image/png"}, "", "text/html"},
44 {"text/html, image/png", []string{"image/png", "text/html"}, "", "image/png"},
45 {"text/html, image/png; q=0.5", []string{"image/png"}, "", "image/png"},
46 {"text/html, image/png; q=0.5", []string{"text/html"}, "", "text/html"},
47 {"text/html, image/png; q=0.5", []string{"foo/bar"}, "", ""},
48 {"text/html, image/png; q=0.5", []string{"image/png", "text/html"}, "", "text/html"},
49 {"text/html, image/png; q=0.5", []string{"text/html", "image/png"}, "", "text/html"},
50 {"text/html;q=0.5, image/png", []string{"image/png"}, "", "image/png"},
51 {"text/html;q=0.5, image/png", []string{"text/html"}, "", "text/html"},
52 {"text/html;q=0.5, image/png", []string{"image/png", "text/html"}, "", "image/png"},
53 {"text/html;q=0.5, image/png", []string{"text/html", "image/png"}, "", "image/png"},
54 {"image/png, image/*;q=0.5", []string{"image/jpg", "image/png"}, "", "image/png"},
55 {"image/png, image/*;q=0.5", []string{"image/jpg"}, "", "image/jpg"},
56 {"image/png, image/*;q=0.5", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
57 {"image/png, image/*", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
58 {"image/png, image/*", []string{"image/gif", "image/jpg"}, "", "image/gif"},
59 {"image/png, image/*", []string{"image/gif", "image/png"}, "", "image/png"},
60 {"image/png, image/*", []string{"image/png", "image/gif"}, "", "image/png"},
61}
62
63func TestNegotiateContentType(t *testing.T) {
64 for _, tt := range negotiateContentTypeTests {
65 r := &http.Request{Header: http.Header{"Accept": {tt.s}}}
66 actual := httputil.NegotiateContentType(r, tt.offers, tt.defaultOffer)
67 if actual != tt.expect {
68 t.Errorf("NegotiateContentType(%q, %#v, %q)=%q, want %q", tt.s, tt.offers, tt.defaultOffer, actual, tt.expect)
69 }
70 }
71}