-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http, net/textproto: add Header.Values, MIMEHeader.Values methods
Fixes #34799 Change-Id: I134b2717fa90c8955902e7eeaaf8510dcc28340e Reviewed-on: https://go-review.googlesource.com/c/go/+/200760 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
- Loading branch information
Showing
4 changed files
with
78 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2010 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package textproto | ||
|
||
import "testing" | ||
|
||
type canonicalHeaderKeyTest struct { | ||
in, out string | ||
} | ||
|
||
var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{ | ||
{"a-b-c", "A-B-C"}, | ||
{"a-1-c", "A-1-C"}, | ||
{"User-Agent", "User-Agent"}, | ||
{"uSER-aGENT", "User-Agent"}, | ||
{"user-agent", "User-Agent"}, | ||
{"USER-AGENT", "User-Agent"}, | ||
|
||
// Other valid tchar bytes in tokens: | ||
{"foo-bar_baz", "Foo-Bar_baz"}, | ||
{"foo-bar$baz", "Foo-Bar$baz"}, | ||
{"foo-bar~baz", "Foo-Bar~baz"}, | ||
{"foo-bar*baz", "Foo-Bar*baz"}, | ||
|
||
// Non-ASCII or anything with spaces or non-token chars is unchanged: | ||
{"üser-agenT", "üser-agenT"}, | ||
{"a B", "a B"}, | ||
|
||
// This caused a panic due to mishandling of a space: | ||
{"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"}, | ||
{"foo bar", "foo bar"}, | ||
} | ||
|
||
func TestCanonicalMIMEHeaderKey(t *testing.T) { | ||
for _, tt := range canonicalHeaderKeyTests { | ||
if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out { | ||
t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out) | ||
} | ||
} | ||
} | ||
|
||
// Issue #34799 add a Header method to get multiple values []string, with canonicalized key | ||
func TestMIMEHeaderMultipleValues(t *testing.T) { | ||
testHeader := MIMEHeader{ | ||
"Set-Cookie": {"cookie 1", "cookie 2"}, | ||
} | ||
values := testHeader.Values("set-cookie") | ||
n := len(values) | ||
if n != 2 { | ||
t.Errorf("count: %d; want 2", n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters