i) lower case metedata key names; ii) make binary encoding consistent with other impls.
diff --git a/metadata/metadata.go b/metadata/metadata.go
index adebc38..9e39b1a 100644
--- a/metadata/metadata.go
+++ b/metadata/metadata.go
@@ -61,12 +61,12 @@
// 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 @@
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
diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go
index ef3a1b8..ad783d2 100644
--- a/metadata/metadata_test.go
+++ b/metadata/metadata_test.go
@@ -40,6 +40,27 @@
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 @@
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 @@
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...)