* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / net / http / header_test.go
blob584f1005440934fe804de2d9c8138e8b84a9fbe6
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.
5 package http
7 import (
8 "bytes"
9 "runtime"
10 "testing"
11 "time"
14 var headerWriteTests = []struct {
15 h Header
16 exclude map[string]bool
17 expected string
19 {Header{}, nil, ""},
21 Header{
22 "Content-Type": {"text/html; charset=UTF-8"},
23 "Content-Length": {"0"},
25 nil,
26 "Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
29 Header{
30 "Content-Length": {"0", "1", "2"},
32 nil,
33 "Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
36 Header{
37 "Expires": {"-1"},
38 "Content-Length": {"0"},
39 "Content-Encoding": {"gzip"},
41 map[string]bool{"Content-Length": true},
42 "Content-Encoding: gzip\r\nExpires: -1\r\n",
45 Header{
46 "Expires": {"-1"},
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",
54 Header{
55 "Expires": {"-1"},
56 "Content-Length": {"0"},
57 "Content-Encoding": {"gzip"},
59 map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
60 "",
63 Header{
64 "Nil": nil,
65 "Empty": {},
66 "Blank": {""},
67 "Double-Blank": {"", ""},
69 nil,
70 "Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
72 // Tests header sorting when over the insertion sort threshold side:
74 Header{
75 "k1": {"1a", "1b"},
76 "k2": {"2a", "2b"},
77 "k3": {"3a", "3b"},
78 "k4": {"4a", "4b"},
79 "k5": {"5a", "5b"},
80 "k6": {"6a", "6b"},
81 "k7": {"7a", "7b"},
82 "k8": {"8a", "8b"},
83 "k9": {"9a", "9b"},
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) {
93 var buf bytes.Buffer
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)
99 buf.Reset()
103 var parseTimeTests = []struct {
104 h Header
105 err bool
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"))
119 if err != nil {
120 if !test.err {
121 t.Errorf("#%d:\n got err: %v", i, err)
123 continue
125 if test.err {
126 t.Errorf("#%d:\n should err", i)
127 continue
129 if !expect.Equal(d) {
130 t.Errorf("#%d:\n got: %v\nwant: %v", i, d, expect)
135 type hasTokenTest struct {
136 header string
137 token string
138 want bool
141 var hasTokenTests = []hasTokenTest{
142 {"", "", false},
143 {"", "foo", false},
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},
185 var buf bytes.Buffer
187 func BenchmarkHeaderWriteSubset(b *testing.B) {
188 b.ReportAllocs()
189 for i := 0; i < b.N; i++ {
190 buf.Reset()
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() {
201 buf.Reset()
202 testHeader.WriteSubset(&buf, nil)
204 if n > 0 {
205 t.Errorf("mallocs = %d; want 0", n)