aboutsummaryrefslogtreecommitdiff
path: root/httputil/header/header_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'httputil/header/header_test.go')
-rw-r--r--httputil/header/header_test.go139
1 files changed, 139 insertions, 0 deletions
diff --git a/httputil/header/header_test.go b/httputil/header/header_test.go
new file mode 100644
index 0000000..97c8685
--- /dev/null
+++ b/httputil/header/header_test.go
@@ -0,0 +1,139 @@
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 header
8
9import (
10 "net/http"
11 "testing"
12 "time"
13
14 "github.com/google/go-cmp/cmp"
15)
16
17var getHeaderListTests = []struct {
18 s string
19 l []string
20}{
21 {s: `a`, l: []string{`a`}},
22 {s: `a, b , c `, l: []string{`a`, `b`, `c`}},
23 {s: `a,, b , , c `, l: []string{`a`, `b`, `c`}},
24 {s: `a,b,c`, l: []string{`a`, `b`, `c`}},
25 {s: ` a b, c d `, l: []string{`a b`, `c d`}},
26 {s: `"a, b, c", d `, l: []string{`"a, b, c"`, "d"}},
27 {s: `","`, l: []string{`","`}},
28 {s: `"\""`, l: []string{`"\""`}},
29 {s: `" "`, l: []string{`" "`}},
30}
31
32func TestGetHeaderList(t *testing.T) {
33 for _, tt := range getHeaderListTests {
34 header := http.Header{"Foo": {tt.s}}
35 if l := ParseList(header, "foo"); !cmp.Equal(tt.l, l) {
36 t.Errorf("ParseList for %q = %q, want %q", tt.s, l, tt.l)
37 }
38 }
39}
40
41var parseValueAndParamsTests = []struct {
42 s string
43 value string
44 params map[string]string
45}{
46 {`text/html`, "text/html", map[string]string{}},
47 {`text/html `, "text/html", map[string]string{}},
48 {`text/html ; `, "text/html", map[string]string{}},
49 {`tExt/htMl`, "text/html", map[string]string{}},
50 {`tExt/htMl; fOO=";"; hellO=world`, "text/html", map[string]string{
51 "hello": "world",
52 "foo": `;`,
53 }},
54 {`text/html; foo=bar, hello=world`, "text/html", map[string]string{"foo": "bar"}},
55 {`text/html ; foo=bar `, "text/html", map[string]string{"foo": "bar"}},
56 {`text/html ;foo=bar `, "text/html", map[string]string{"foo": "bar"}},
57 {`text/html; foo="b\ar"`, "text/html", map[string]string{"foo": "bar"}},
58 {`text/html; foo="bar\"baz\"qux"`, "text/html", map[string]string{"foo": `bar"baz"qux`}},
59 {`text/html; foo="b,ar"`, "text/html", map[string]string{"foo": "b,ar"}},
60 {`text/html; foo="b;ar"`, "text/html", map[string]string{"foo": "b;ar"}},
61 {`text/html; FOO="bar"`, "text/html", map[string]string{"foo": "bar"}},
62 {`form-data; filename="file.txt"; name=file`, "form-data", map[string]string{"filename": "file.txt", "name": "file"}},
63}
64
65func TestParseValueAndParams(t *testing.T) {
66 for _, tt := range parseValueAndParamsTests {
67 header := http.Header{"Content-Type": {tt.s}}
68 value, params := ParseValueAndParams(header, "Content-Type")
69 if value != tt.value {
70 t.Errorf("%q, value=%q, want %q", tt.s, value, tt.value)
71 }
72 if !cmp.Equal(params, tt.params) {
73 t.Errorf("%q, param=%#v, want %#v", tt.s, params, tt.params)
74 }
75 }
76}
77
78var parseTimeValidTests = []string{
79 "Sun, 06 Nov 1994 08:49:37 GMT",
80 "Sunday, 06-Nov-94 08:49:37 GMT",
81 "Sun Nov 6 08:49:37 1994",
82}
83
84var parseTimeInvalidTests = []string{
85 "junk",
86}
87
88func TestParseTime(t *testing.T) {
89 expected := time.Date(1994, 11, 6, 8, 49, 37, 0, time.UTC)
90 for _, s := range parseTimeValidTests {
91 header := http.Header{"Date": {s}}
92 actual := ParseTime(header, "Date")
93 if actual != expected {
94 t.Errorf("GetTime(%q)=%v, want %v", s, actual, expected)
95 }
96 }
97 for _, s := range parseTimeInvalidTests {
98 header := http.Header{"Date": {s}}
99 actual := ParseTime(header, "Date")
100 if !actual.IsZero() {
101 t.Errorf("GetTime(%q) did not return zero", s)
102 }
103 }
104}
105
106var parseAcceptTests = []struct {
107 s string
108 expected []AcceptSpec
109}{
110 {"text/html", []AcceptSpec{{"text/html", 1}}},
111 {"text/html; q=0", []AcceptSpec{{"text/html", 0}}},
112 {"text/html; q=0.0", []AcceptSpec{{"text/html", 0}}},
113 {"text/html; q=1", []AcceptSpec{{"text/html", 1}}},
114 {"text/html; q=1.0", []AcceptSpec{{"text/html", 1}}},
115 {"text/html; q=0.1", []AcceptSpec{{"text/html", 0.1}}},
116 {"text/html;q=0.1", []AcceptSpec{{"text/html", 0.1}}},
117 {"text/html, text/plain", []AcceptSpec{{"text/html", 1}, {"text/plain", 1}}},
118 {"text/html; q=0.1, text/plain", []AcceptSpec{{"text/html", 0.1}, {"text/plain", 1}}},
119 {"iso-8859-5, unicode-1-1;q=0.8,iso-8859-1", []AcceptSpec{{"iso-8859-5", 1}, {"unicode-1-1", 0.8}, {"iso-8859-1", 1}}},
120 {"iso-8859-1", []AcceptSpec{{"iso-8859-1", 1}}},
121 {"*", []AcceptSpec{{"*", 1}}},
122 {"da, en-gb;q=0.8, en;q=0.7", []AcceptSpec{{"da", 1}, {"en-gb", 0.8}, {"en", 0.7}}},
123 {"da, q, en-gb;q=0.8", []AcceptSpec{{"da", 1}, {"q", 1}, {"en-gb", 0.8}}},
124 {"image/png, image/*;q=0.5", []AcceptSpec{{"image/png", 1}, {"image/*", 0.5}}},
125
126 // bad cases
127 {"value1; q=0.1.2", []AcceptSpec{{"value1", 0.1}}},
128 {"da, en-gb;q=foo", []AcceptSpec{{"da", 1}}},
129}
130
131func TestParseAccept(t *testing.T) {
132 for _, tt := range parseAcceptTests {
133 header := http.Header{"Accept": {tt.s}}
134 actual := ParseAccept(header, "Accept")
135 if !cmp.Equal(actual, tt.expected) {
136 t.Errorf("ParseAccept(h, %q)=%v, want %v", tt.s, actual, tt.expected)
137 }
138 }
139}