github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/net/textproto/reader_test.go (about) 1 // Copyright 2010 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 textproto 6 7 import ( 8 "bufio" 9 "bytes" 10 "io" 11 "reflect" 12 "strings" 13 "testing" 14 ) 15 16 type canonicalHeaderKeyTest struct { 17 in, out string 18 } 19 20 var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{ 21 {"a-b-c", "A-B-C"}, 22 {"a-1-c", "A-1-C"}, 23 {"User-Agent", "User-Agent"}, 24 {"uSER-aGENT", "User-Agent"}, 25 {"user-agent", "User-Agent"}, 26 {"USER-AGENT", "User-Agent"}, 27 28 // Non-ASCII or anything with spaces or non-token chars is unchanged: 29 {"üser-agenT", "üser-agenT"}, 30 {"a B", "a B"}, 31 32 // This caused a panic due to mishandling of a space: 33 {"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"}, 34 {"foo bar", "foo bar"}, 35 } 36 37 func TestCanonicalMIMEHeaderKey(t *testing.T) { 38 for _, tt := range canonicalHeaderKeyTests { 39 if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out { 40 t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out) 41 } 42 } 43 } 44 45 func reader(s string) *Reader { 46 return NewReader(bufio.NewReader(strings.NewReader(s))) 47 } 48 49 func TestReadLine(t *testing.T) { 50 r := reader("line1\nline2\n") 51 s, err := r.ReadLine() 52 if s != "line1" || err != nil { 53 t.Fatalf("Line 1: %s, %v", s, err) 54 } 55 s, err = r.ReadLine() 56 if s != "line2" || err != nil { 57 t.Fatalf("Line 2: %s, %v", s, err) 58 } 59 s, err = r.ReadLine() 60 if s != "" || err != io.EOF { 61 t.Fatalf("EOF: %s, %v", s, err) 62 } 63 } 64 65 func TestReadContinuedLine(t *testing.T) { 66 r := reader("line1\nline\n 2\nline3\n") 67 s, err := r.ReadContinuedLine() 68 if s != "line1" || err != nil { 69 t.Fatalf("Line 1: %s, %v", s, err) 70 } 71 s, err = r.ReadContinuedLine() 72 if s != "line 2" || err != nil { 73 t.Fatalf("Line 2: %s, %v", s, err) 74 } 75 s, err = r.ReadContinuedLine() 76 if s != "line3" || err != nil { 77 t.Fatalf("Line 3: %s, %v", s, err) 78 } 79 s, err = r.ReadContinuedLine() 80 if s != "" || err != io.EOF { 81 t.Fatalf("EOF: %s, %v", s, err) 82 } 83 } 84 85 func TestReadCodeLine(t *testing.T) { 86 r := reader("123 hi\n234 bye\n345 no way\n") 87 code, msg, err := r.ReadCodeLine(0) 88 if code != 123 || msg != "hi" || err != nil { 89 t.Fatalf("Line 1: %d, %s, %v", code, msg, err) 90 } 91 code, msg, err = r.ReadCodeLine(23) 92 if code != 234 || msg != "bye" || err != nil { 93 t.Fatalf("Line 2: %d, %s, %v", code, msg, err) 94 } 95 code, msg, err = r.ReadCodeLine(346) 96 if code != 345 || msg != "no way" || err == nil { 97 t.Fatalf("Line 3: %d, %s, %v", code, msg, err) 98 } 99 if e, ok := err.(*Error); !ok || e.Code != code || e.Msg != msg { 100 t.Fatalf("Line 3: wrong error %v\n", err) 101 } 102 code, msg, err = r.ReadCodeLine(1) 103 if code != 0 || msg != "" || err != io.EOF { 104 t.Fatalf("EOF: %d, %s, %v", code, msg, err) 105 } 106 } 107 108 func TestReadDotLines(t *testing.T) { 109 r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanother\n") 110 s, err := r.ReadDotLines() 111 want := []string{"dotlines", "foo", ".bar", "..baz", "quux", ""} 112 if !reflect.DeepEqual(s, want) || err != nil { 113 t.Fatalf("ReadDotLines: %v, %v", s, err) 114 } 115 116 s, err = r.ReadDotLines() 117 want = []string{"another"} 118 if !reflect.DeepEqual(s, want) || err != io.ErrUnexpectedEOF { 119 t.Fatalf("ReadDotLines2: %v, %v", s, err) 120 } 121 } 122 123 func TestReadDotBytes(t *testing.T) { 124 r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n") 125 b, err := r.ReadDotBytes() 126 want := []byte("dotlines\nfoo\n.bar\n..baz\nquux\n\n") 127 if !reflect.DeepEqual(b, want) || err != nil { 128 t.Fatalf("ReadDotBytes: %q, %v", b, err) 129 } 130 131 b, err = r.ReadDotBytes() 132 want = []byte("anot.her\n") 133 if !reflect.DeepEqual(b, want) || err != io.ErrUnexpectedEOF { 134 t.Fatalf("ReadDotBytes2: %q, %v", b, err) 135 } 136 } 137 138 func TestReadMIMEHeader(t *testing.T) { 139 r := reader("my-key: Value 1 \r\nLong-key: Even \n Longer Value\r\nmy-Key: Value 2\r\n\n") 140 m, err := r.ReadMIMEHeader() 141 want := MIMEHeader{ 142 "My-Key": {"Value 1", "Value 2"}, 143 "Long-Key": {"Even Longer Value"}, 144 } 145 if !reflect.DeepEqual(m, want) || err != nil { 146 t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want) 147 } 148 } 149 150 func TestReadMIMEHeaderSingle(t *testing.T) { 151 r := reader("Foo: bar\n\n") 152 m, err := r.ReadMIMEHeader() 153 want := MIMEHeader{"Foo": {"bar"}} 154 if !reflect.DeepEqual(m, want) || err != nil { 155 t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want) 156 } 157 } 158 159 func TestReadMIMEHeaderNoKey(t *testing.T) { 160 r := reader(": bar\ntest-1: 1\n\n") 161 m, err := r.ReadMIMEHeader() 162 want := MIMEHeader{"Test-1": {"1"}} 163 if !reflect.DeepEqual(m, want) || err != nil { 164 t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want) 165 } 166 } 167 168 func TestLargeReadMIMEHeader(t *testing.T) { 169 data := make([]byte, 16*1024) 170 for i := 0; i < len(data); i++ { 171 data[i] = 'x' 172 } 173 sdata := string(data) 174 r := reader("Cookie: " + sdata + "\r\n\n") 175 m, err := r.ReadMIMEHeader() 176 if err != nil { 177 t.Fatalf("ReadMIMEHeader: %v", err) 178 } 179 cookie := m.Get("Cookie") 180 if cookie != sdata { 181 t.Fatalf("ReadMIMEHeader: %v bytes, want %v bytes", len(cookie), len(sdata)) 182 } 183 } 184 185 // Test that we read slightly-bogus MIME headers seen in the wild, 186 // with spaces before colons, and spaces in keys. 187 func TestReadMIMEHeaderNonCompliant(t *testing.T) { 188 // Invalid HTTP response header as sent by an Axis security 189 // camera: (this is handled by IE, Firefox, Chrome, curl, etc.) 190 r := reader("Foo: bar\r\n" + 191 "Content-Language: en\r\n" + 192 "SID : 0\r\n" + 193 "Audio Mode : None\r\n" + 194 "Privilege : 127\r\n\r\n") 195 m, err := r.ReadMIMEHeader() 196 want := MIMEHeader{ 197 "Foo": {"bar"}, 198 "Content-Language": {"en"}, 199 "Sid": {"0"}, 200 "Audio Mode": {"None"}, 201 "Privilege": {"127"}, 202 } 203 if !reflect.DeepEqual(m, want) || err != nil { 204 t.Fatalf("ReadMIMEHeader =\n%v, %v; want:\n%v", m, err, want) 205 } 206 } 207 208 // Test that continued lines are properly trimmed. Issue 11204. 209 func TestReadMIMEHeaderTrimContinued(t *testing.T) { 210 // In this header, \n and \r\n terminated lines are mixed on purpose. 211 // We expect each line to be trimmed (prefix and suffix) before being concatenated. 212 // Keep the spaces as they are. 213 r := reader("" + // for code formatting purpose. 214 "a:\n" + 215 " 0 \r\n" + 216 "b:1 \t\r\n" + 217 "c: 2\r\n" + 218 " 3\t\n" + 219 " \t 4 \r\n\n") 220 m, err := r.ReadMIMEHeader() 221 if err != nil { 222 t.Fatal(err) 223 } 224 want := MIMEHeader{ 225 "A": {"0"}, 226 "B": {"1"}, 227 "C": {"2 3 4"}, 228 } 229 if !reflect.DeepEqual(m, want) { 230 t.Fatalf("ReadMIMEHeader mismatch.\n got: %q\nwant: %q", m, want) 231 } 232 } 233 234 type readResponseTest struct { 235 in string 236 inCode int 237 wantCode int 238 wantMsg string 239 } 240 241 var readResponseTests = []readResponseTest{ 242 {"230-Anonymous access granted, restrictions apply\n" + 243 "Read the file README.txt,\n" + 244 "230 please", 245 23, 246 230, 247 "Anonymous access granted, restrictions apply\nRead the file README.txt,\n please", 248 }, 249 250 {"230 Anonymous access granted, restrictions apply\n", 251 23, 252 230, 253 "Anonymous access granted, restrictions apply", 254 }, 255 256 {"400-A\n400-B\n400 C", 257 4, 258 400, 259 "A\nB\nC", 260 }, 261 262 {"400-A\r\n400-B\r\n400 C\r\n", 263 4, 264 400, 265 "A\nB\nC", 266 }, 267 } 268 269 // See http://www.ietf.org/rfc/rfc959.txt page 36. 270 func TestRFC959Lines(t *testing.T) { 271 for i, tt := range readResponseTests { 272 r := reader(tt.in + "\nFOLLOWING DATA") 273 code, msg, err := r.ReadResponse(tt.inCode) 274 if err != nil { 275 t.Errorf("#%d: ReadResponse: %v", i, err) 276 continue 277 } 278 if code != tt.wantCode { 279 t.Errorf("#%d: code=%d, want %d", i, code, tt.wantCode) 280 } 281 if msg != tt.wantMsg { 282 t.Errorf("#%d: msg=%q, want %q", i, msg, tt.wantMsg) 283 } 284 } 285 } 286 287 func TestCommonHeaders(t *testing.T) { 288 for h := range commonHeader { 289 if h != CanonicalMIMEHeaderKey(h) { 290 t.Errorf("Non-canonical header %q in commonHeader", h) 291 } 292 } 293 b := []byte("content-Length") 294 want := "Content-Length" 295 n := testing.AllocsPerRun(200, func() { 296 if x := canonicalMIMEHeaderKey(b); x != want { 297 t.Fatalf("canonicalMIMEHeaderKey(%q) = %q; want %q", b, x, want) 298 } 299 }) 300 if n > 0 { 301 t.Errorf("canonicalMIMEHeaderKey allocs = %v; want 0", n) 302 } 303 } 304 305 var clientHeaders = strings.Replace(`Host: golang.org 306 Connection: keep-alive 307 Cache-Control: max-age=0 308 Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 309 User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3 310 Accept-Encoding: gzip,deflate,sdch 311 Accept-Language: en-US,en;q=0.8,fr-CH;q=0.6 312 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 313 COOKIE: __utma=000000000.0000000000.0000000000.0000000000.0000000000.00; __utmb=000000000.0.00.0000000000; __utmc=000000000; __utmz=000000000.0000000000.00.0.utmcsr=code.google.com|utmccn=(referral)|utmcmd=referral|utmcct=/p/go/issues/detail 314 Non-Interned: test 315 316 `, "\n", "\r\n", -1) 317 318 var serverHeaders = strings.Replace(`Content-Type: text/html; charset=utf-8 319 Content-Encoding: gzip 320 Date: Thu, 27 Sep 2012 09:03:33 GMT 321 Server: Google Frontend 322 Cache-Control: private 323 Content-Length: 2298 324 VIA: 1.1 proxy.example.com:80 (XXX/n.n.n-nnn) 325 Connection: Close 326 Non-Interned: test 327 328 `, "\n", "\r\n", -1) 329 330 func BenchmarkReadMIMEHeader(b *testing.B) { 331 b.ReportAllocs() 332 var buf bytes.Buffer 333 br := bufio.NewReader(&buf) 334 r := NewReader(br) 335 for i := 0; i < b.N; i++ { 336 var want int 337 var find string 338 if (i & 1) == 1 { 339 buf.WriteString(clientHeaders) 340 want = 10 341 find = "Cookie" 342 } else { 343 buf.WriteString(serverHeaders) 344 want = 9 345 find = "Via" 346 } 347 h, err := r.ReadMIMEHeader() 348 if err != nil { 349 b.Fatal(err) 350 } 351 if len(h) != want { 352 b.Fatalf("wrong number of headers: got %d, want %d", len(h), want) 353 } 354 if _, ok := h[find]; !ok { 355 b.Fatalf("did not find key %s", find) 356 } 357 } 358 } 359 360 func BenchmarkUncommon(b *testing.B) { 361 b.ReportAllocs() 362 var buf bytes.Buffer 363 br := bufio.NewReader(&buf) 364 r := NewReader(br) 365 for i := 0; i < b.N; i++ { 366 buf.WriteString("uncommon-header-for-benchmark: foo\r\n\r\n") 367 h, err := r.ReadMIMEHeader() 368 if err != nil { 369 b.Fatal(err) 370 } 371 if _, ok := h["Uncommon-Header-For-Benchmark"]; !ok { 372 b.Fatal("Missing result header.") 373 } 374 } 375 }