github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/net/http/header_test.go (about) 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. 4 5 package http 6 7 import ( 8 "bytes" 9 "internal/race" 10 "runtime" 11 "testing" 12 "time" 13 ) 14 15 var headerWriteTests = []struct { 16 h Header 17 exclude map[string]bool 18 expected string 19 }{ 20 {Header{}, nil, ""}, 21 { 22 Header{ 23 "Content-Type": {"text/html; charset=UTF-8"}, 24 "Content-Length": {"0"}, 25 }, 26 nil, 27 "Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n", 28 }, 29 { 30 Header{ 31 "Content-Length": {"0", "1", "2"}, 32 }, 33 nil, 34 "Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n", 35 }, 36 { 37 Header{ 38 "Expires": {"-1"}, 39 "Content-Length": {"0"}, 40 "Content-Encoding": {"gzip"}, 41 }, 42 map[string]bool{"Content-Length": true}, 43 "Content-Encoding: gzip\r\nExpires: -1\r\n", 44 }, 45 { 46 Header{ 47 "Expires": {"-1"}, 48 "Content-Length": {"0", "1", "2"}, 49 "Content-Encoding": {"gzip"}, 50 }, 51 map[string]bool{"Content-Length": true}, 52 "Content-Encoding: gzip\r\nExpires: -1\r\n", 53 }, 54 { 55 Header{ 56 "Expires": {"-1"}, 57 "Content-Length": {"0"}, 58 "Content-Encoding": {"gzip"}, 59 }, 60 map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true}, 61 "", 62 }, 63 { 64 Header{ 65 "Nil": nil, 66 "Empty": {}, 67 "Blank": {""}, 68 "Double-Blank": {"", ""}, 69 }, 70 nil, 71 "Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n", 72 }, 73 // Tests header sorting when over the insertion sort threshold side: 74 { 75 Header{ 76 "k1": {"1a", "1b"}, 77 "k2": {"2a", "2b"}, 78 "k3": {"3a", "3b"}, 79 "k4": {"4a", "4b"}, 80 "k5": {"5a", "5b"}, 81 "k6": {"6a", "6b"}, 82 "k7": {"7a", "7b"}, 83 "k8": {"8a", "8b"}, 84 "k9": {"9a", "9b"}, 85 }, 86 map[string]bool{"k5": true}, 87 "k1: 1a\r\nk1: 1b\r\nk2: 2a\r\nk2: 2b\r\nk3: 3a\r\nk3: 3b\r\n" + 88 "k4: 4a\r\nk4: 4b\r\nk6: 6a\r\nk6: 6b\r\n" + 89 "k7: 7a\r\nk7: 7b\r\nk8: 8a\r\nk8: 8b\r\nk9: 9a\r\nk9: 9b\r\n", 90 }, 91 } 92 93 func TestHeaderWrite(t *testing.T) { 94 var buf bytes.Buffer 95 for i, test := range headerWriteTests { 96 test.h.WriteSubset(&buf, test.exclude) 97 if buf.String() != test.expected { 98 t.Errorf("#%d:\n got: %q\nwant: %q", i, buf.String(), test.expected) 99 } 100 buf.Reset() 101 } 102 } 103 104 var parseTimeTests = []struct { 105 h Header 106 err bool 107 }{ 108 {Header{"Date": {""}}, true}, 109 {Header{"Date": {"invalid"}}, true}, 110 {Header{"Date": {"1994-11-06T08:49:37Z00:00"}}, true}, 111 {Header{"Date": {"Sun, 06 Nov 1994 08:49:37 GMT"}}, false}, 112 {Header{"Date": {"Sunday, 06-Nov-94 08:49:37 GMT"}}, false}, 113 {Header{"Date": {"Sun Nov 6 08:49:37 1994"}}, false}, 114 } 115 116 func TestParseTime(t *testing.T) { 117 expect := time.Date(1994, 11, 6, 8, 49, 37, 0, time.UTC) 118 for i, test := range parseTimeTests { 119 d, err := ParseTime(test.h.Get("Date")) 120 if err != nil { 121 if !test.err { 122 t.Errorf("#%d:\n got err: %v", i, err) 123 } 124 continue 125 } 126 if test.err { 127 t.Errorf("#%d:\n should err", i) 128 continue 129 } 130 if !expect.Equal(d) { 131 t.Errorf("#%d:\n got: %v\nwant: %v", i, d, expect) 132 } 133 } 134 } 135 136 type hasTokenTest struct { 137 header string 138 token string 139 want bool 140 } 141 142 var hasTokenTests = []hasTokenTest{ 143 {"", "", false}, 144 {"", "foo", false}, 145 {"foo", "foo", true}, 146 {"foo ", "foo", true}, 147 {" foo", "foo", true}, 148 {" foo ", "foo", true}, 149 {"foo,bar", "foo", true}, 150 {"bar,foo", "foo", true}, 151 {"bar, foo", "foo", true}, 152 {"bar,foo, baz", "foo", true}, 153 {"bar, foo,baz", "foo", true}, 154 {"bar,foo, baz", "foo", true}, 155 {"bar, foo, baz", "foo", true}, 156 {"FOO", "foo", true}, 157 {"FOO ", "foo", true}, 158 {" FOO", "foo", true}, 159 {" FOO ", "foo", true}, 160 {"FOO,BAR", "foo", true}, 161 {"BAR,FOO", "foo", true}, 162 {"BAR, FOO", "foo", true}, 163 {"BAR,FOO, baz", "foo", true}, 164 {"BAR, FOO,BAZ", "foo", true}, 165 {"BAR,FOO, BAZ", "foo", true}, 166 {"BAR, FOO, BAZ", "foo", true}, 167 {"foobar", "foo", false}, 168 {"barfoo ", "foo", false}, 169 } 170 171 func TestHasToken(t *testing.T) { 172 for _, tt := range hasTokenTests { 173 if hasToken(tt.header, tt.token) != tt.want { 174 t.Errorf("hasToken(%q, %q) = %v; want %v", tt.header, tt.token, !tt.want, tt.want) 175 } 176 } 177 } 178 179 var testHeader = Header{ 180 "Content-Length": {"123"}, 181 "Content-Type": {"text/plain"}, 182 "Date": {"some date at some time Z"}, 183 "Server": {DefaultUserAgent}, 184 } 185 186 var buf bytes.Buffer 187 188 func BenchmarkHeaderWriteSubset(b *testing.B) { 189 b.ReportAllocs() 190 for i := 0; i < b.N; i++ { 191 buf.Reset() 192 testHeader.WriteSubset(&buf, nil) 193 } 194 } 195 196 func TestHeaderWriteSubsetAllocs(t *testing.T) { 197 if testing.Short() { 198 t.Skip("skipping alloc test in short mode") 199 } 200 if race.Enabled { 201 t.Skip("skipping test under race detector") 202 } 203 if runtime.GOMAXPROCS(0) > 1 { 204 t.Skip("skipping; GOMAXPROCS>1") 205 } 206 n := testing.AllocsPerRun(100, func() { 207 buf.Reset() 208 testHeader.WriteSubset(&buf, nil) 209 }) 210 if n > 0 { 211 t.Errorf("allocs = %g; want 0", n) 212 } 213 }