Skip to content

Commit

Permalink
i) lower case metedata key names; ii) make binary encoding consistent…
Browse files Browse the repository at this point in the history
… with other impls.
  • Loading branch information
iamqizhao committed Dec 15, 2015
1 parent 2d76f2f commit 87d8411
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
13 changes: 6 additions & 7 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 25 additions & 4 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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...)
Expand Down

0 comments on commit 87d8411

Please sign in to comment.