github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/textproto/header_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 "testing" 8 9 type canonicalHeaderKeyTest struct { 10 in, out string 11 } 12 13 var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{ 14 {"a-b-c", "A-B-C"}, 15 {"a-1-c", "A-1-C"}, 16 {"User-Agent", "User-Agent"}, 17 {"uSER-aGENT", "User-Agent"}, 18 {"user-agent", "User-Agent"}, 19 {"USER-AGENT", "User-Agent"}, 20 21 // Other valid tchar bytes in tokens: 22 {"foo-bar_baz", "Foo-Bar_baz"}, 23 {"foo-bar$baz", "Foo-Bar$baz"}, 24 {"foo-bar~baz", "Foo-Bar~baz"}, 25 {"foo-bar*baz", "Foo-Bar*baz"}, 26 27 // Non-ASCII or anything with spaces or non-token chars is unchanged: 28 {"üser-agenT", "üser-agenT"}, 29 {"a B", "a B"}, 30 31 // This caused a panic due to mishandling of a space: 32 {"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"}, 33 {"foo bar", "foo bar"}, 34 } 35 36 func TestCanonicalMIMEHeaderKey(t *testing.T) { 37 for _, tt := range canonicalHeaderKeyTests { 38 if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out { 39 t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out) 40 } 41 } 42 } 43 44 // Issue #34799 add a Header method to get multiple values []string, with canonicalized key 45 func TestMIMEHeaderMultipleValues(t *testing.T) { 46 testHeader := MIMEHeader{ 47 "Set-Cookie": {"cookie 1", "cookie 2"}, 48 } 49 values := testHeader.Values("set-cookie") 50 n := len(values) 51 if n != 2 { 52 t.Errorf("count: %d; want 2", n) 53 } 54 }