i) lower case metedata key names; ii) make binary encoding consistent with other impls.
This commit is contained in:
Родитель
2d76f2fb90
Коммит
87d84117a8
|
@ -61,12 +61,12 @@ func isASCII(s string) bool {
|
|||
// Transmitting binary headers violates HTTP/2 spec.
|
||||
// TODO(zhaoq): Maybe check if k is ASCII also.
|
||||
func encodeKeyValue(k, v string) (string, string) {
|
||||
if isASCII(v) {
|
||||
return k, v
|
||||
k = strings.ToLower(k)
|
||||
if strings.HasSuffix(k, binHdrSuffix) {
|
||||
val := base64.StdEncoding.EncodeToString([]byte(v))
|
||||
v = string(val)
|
||||
}
|
||||
key := strings.ToLower(k + binHdrSuffix)
|
||||
val := base64.StdEncoding.EncodeToString([]byte(v))
|
||||
return key, string(val)
|
||||
return k, v
|
||||
}
|
||||
|
||||
// DecodeKeyValue returns the original key and value corresponding to the
|
||||
|
@ -75,12 +75,11 @@ func DecodeKeyValue(k, v string) (string, string, error) {
|
|||
if !strings.HasSuffix(k, binHdrSuffix) {
|
||||
return k, v, nil
|
||||
}
|
||||
key := k[:len(k)-len(binHdrSuffix)]
|
||||
val, err := base64.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return key, string(val), nil
|
||||
return k, string(val), nil
|
||||
}
|
||||
|
||||
// MD is a mapping from metadata keys to values. Users should use the following
|
||||
|
|
|
@ -40,6 +40,27 @@ import (
|
|||
|
||||
const binaryValue = string(128)
|
||||
|
||||
func TestEncodeKeyValue(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
// input
|
||||
kin string
|
||||
vin string
|
||||
// output
|
||||
kout string
|
||||
vout string
|
||||
}{
|
||||
{"key", "abc", "key", "abc"},
|
||||
{"KEY", "abc", "key", "abc"},
|
||||
{"key-bin", "abc", "key-bin", "YWJj"},
|
||||
{"key-bin", binaryValue, "key-bin", "woA="},
|
||||
} {
|
||||
k, v := encodeKeyValue(test.kin, test.vin)
|
||||
if k != test.kout || !reflect.DeepEqual(v, test.vout) {
|
||||
t.Fatalf("encodeKeyValue(%q, %q) = %q, %q, want %q, %q", test.kin, test.vin, k, v, test.kout, test.vout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeKeyValue(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
// input
|
||||
|
@ -51,8 +72,8 @@ func TestDecodeKeyValue(t *testing.T) {
|
|||
err error
|
||||
}{
|
||||
{"a", "abc", "a", "abc", nil},
|
||||
{"key-bin", "Zm9vAGJhcg==", "key", "foo\x00bar", nil},
|
||||
{"key-bin", "woA=", "key", binaryValue, nil},
|
||||
{"key-bin", "Zm9vAGJhcg==", "key-bin", "foo\x00bar", nil},
|
||||
{"key-bin", "woA=", "key-bin", binaryValue, nil},
|
||||
} {
|
||||
k, v, err := DecodeKeyValue(test.kin, test.vin)
|
||||
if k != test.kout || !reflect.DeepEqual(v, test.vout) || !reflect.DeepEqual(err, test.err) {
|
||||
|
@ -69,9 +90,9 @@ func TestPairsMD(t *testing.T) {
|
|||
md MD
|
||||
}{
|
||||
{[]string{}, MD{}},
|
||||
{[]string{"k1", "v1", "k2", binaryValue}, New(map[string]string{
|
||||
{[]string{"k1", "v1", "k2-bin", binaryValue}, New(map[string]string{
|
||||
"k1": "v1",
|
||||
"k2-bin": "woA=",
|
||||
"k2-bin": binaryValue,
|
||||
})},
|
||||
} {
|
||||
md := Pairs(test.kv...)
|
||||
|
|
Загрузка…
Ссылка в новой задаче