1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var headerWriteTests
= []struct {
16 exclude
map[string]bool
22 "Content-Type": {"text/html; charset=UTF-8"},
23 "Content-Length": {"0"},
26 "Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
30 "Content-Length": {"0", "1", "2"},
33 "Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
38 "Content-Length": {"0"},
39 "Content-Encoding": {"gzip"},
41 map[string]bool{"Content-Length": true},
42 "Content-Encoding: gzip\r\nExpires: -1\r\n",
47 "Content-Length": {"0", "1", "2"},
48 "Content-Encoding": {"gzip"},
50 map[string]bool{"Content-Length": true},
51 "Content-Encoding: gzip\r\nExpires: -1\r\n",
56 "Content-Length": {"0"},
57 "Content-Encoding": {"gzip"},
59 map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
67 "Double-Blank": {"", ""},
70 "Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
72 // Tests header sorting when over the insertion sort threshold side:
85 map[string]bool{"k5": true},
86 "k1: 1a\r\nk1: 1b\r\nk2: 2a\r\nk2: 2b\r\nk3: 3a\r\nk3: 3b\r\n" +
87 "k4: 4a\r\nk4: 4b\r\nk6: 6a\r\nk6: 6b\r\n" +
88 "k7: 7a\r\nk7: 7b\r\nk8: 8a\r\nk8: 8b\r\nk9: 9a\r\nk9: 9b\r\n",
92 func TestHeaderWrite(t
*testing
.T
) {
94 for i
, test
:= range headerWriteTests
{
95 test
.h
.WriteSubset(&buf
, test
.exclude
)
96 if buf
.String() != test
.expected
{
97 t
.Errorf("#%d:\n got: %q\nwant: %q", i
, buf
.String(), test
.expected
)
103 var parseTimeTests
= []struct {
107 {Header
{"Date": {""}}, true},
108 {Header
{"Date": {"invalid"}}, true},
109 {Header
{"Date": {"1994-11-06T08:49:37Z00:00"}}, true},
110 {Header
{"Date": {"Sun, 06 Nov 1994 08:49:37 GMT"}}, false},
111 {Header
{"Date": {"Sunday, 06-Nov-94 08:49:37 GMT"}}, false},
112 {Header
{"Date": {"Sun Nov 6 08:49:37 1994"}}, false},
115 func TestParseTime(t
*testing
.T
) {
116 expect
:= time
.Date(1994, 11, 6, 8, 49, 37, 0, time
.UTC
)
117 for i
, test
:= range parseTimeTests
{
118 d
, err
:= ParseTime(test
.h
.Get("Date"))
121 t
.Errorf("#%d:\n got err: %v", i
, err
)
126 t
.Errorf("#%d:\n should err", i
)
129 if !expect
.Equal(d
) {
130 t
.Errorf("#%d:\n got: %v\nwant: %v", i
, d
, expect
)
135 type hasTokenTest
struct {
141 var hasTokenTests
= []hasTokenTest
{
144 {"foo", "foo", true},
145 {"foo ", "foo", true},
146 {" foo", "foo", true},
147 {" foo ", "foo", true},
148 {"foo,bar", "foo", true},
149 {"bar,foo", "foo", true},
150 {"bar, foo", "foo", true},
151 {"bar,foo, baz", "foo", true},
152 {"bar, foo,baz", "foo", true},
153 {"bar,foo, baz", "foo", true},
154 {"bar, foo, baz", "foo", true},
155 {"FOO", "foo", true},
156 {"FOO ", "foo", true},
157 {" FOO", "foo", true},
158 {" FOO ", "foo", true},
159 {"FOO,BAR", "foo", true},
160 {"BAR,FOO", "foo", true},
161 {"BAR, FOO", "foo", true},
162 {"BAR,FOO, baz", "foo", true},
163 {"BAR, FOO,BAZ", "foo", true},
164 {"BAR,FOO, BAZ", "foo", true},
165 {"BAR, FOO, BAZ", "foo", true},
166 {"foobar", "foo", false},
167 {"barfoo ", "foo", false},
170 func TestHasToken(t
*testing
.T
) {
171 for _
, tt
:= range hasTokenTests
{
172 if hasToken(tt
.header
, tt
.token
) != tt
.want
{
173 t
.Errorf("hasToken(%q, %q) = %v; want %v", tt
.header
, tt
.token
, !tt
.want
, tt
.want
)
178 var testHeader
= Header
{
179 "Content-Length": {"123"},
180 "Content-Type": {"text/plain"},
181 "Date": {"some date at some time Z"},
182 "Server": {DefaultUserAgent
},
187 func BenchmarkHeaderWriteSubset(b
*testing
.B
) {
189 for i
:= 0; i
< b
.N
; i
++ {
191 testHeader
.WriteSubset(&buf
, nil)
195 func TestHeaderWriteSubsetMallocs(t
*testing
.T
) {
196 t
.Skip("Skipping alloc count test on gccgo")
197 if runtime
.GOMAXPROCS(0) > 1 {
198 t
.Skip("skipping; GOMAXPROCS>1")
200 n
:= testing
.AllocsPerRun(100, func() {
202 testHeader
.WriteSubset(&buf
, nil)
205 t
.Errorf("mallocs = %d; want 0", n
)