2016-09-12 07:25:09 +03:00
|
|
|
/*
|
|
|
|
*
|
2017-10-05 17:40:19 +03:00
|
|
|
* Copyright 2014 gRPC authors.
|
2016-09-12 07:25:09 +03:00
|
|
|
*
|
2017-10-05 17:40:19 +03:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2016-09-12 07:25:09 +03:00
|
|
|
*
|
2017-10-05 17:40:19 +03:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-09-12 07:25:09 +03:00
|
|
|
*
|
2017-10-05 17:40:19 +03:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2016-09-12 07:25:09 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-10-05 17:40:19 +03:00
|
|
|
func TestPairsMD(t *testing.T) {
|
2016-09-12 07:25:09 +03:00
|
|
|
for _, test := range []struct {
|
|
|
|
// input
|
2017-10-05 17:40:19 +03:00
|
|
|
kv []string
|
2016-09-12 07:25:09 +03:00
|
|
|
// output
|
2017-10-05 17:40:19 +03:00
|
|
|
md MD
|
2016-09-12 07:25:09 +03:00
|
|
|
}{
|
2017-10-05 17:40:19 +03:00
|
|
|
{[]string{}, MD{}},
|
|
|
|
{[]string{"k1", "v1", "k1", "v2"}, MD{"k1": []string{"v1", "v2"}}},
|
2016-09-12 07:25:09 +03:00
|
|
|
} {
|
2017-10-05 17:40:19 +03:00
|
|
|
md := Pairs(test.kv...)
|
|
|
|
if !reflect.DeepEqual(md, test.md) {
|
|
|
|
t.Fatalf("Pairs(%v) = %v, want %v", test.kv, md, test.md)
|
2016-09-12 07:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:40:19 +03:00
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
const key, val = "key", "val"
|
|
|
|
orig := Pairs(key, val)
|
|
|
|
copy := orig.Copy()
|
|
|
|
if !reflect.DeepEqual(orig, copy) {
|
|
|
|
t.Errorf("copied value not equal to the original, got %v, want %v", copy, orig)
|
|
|
|
}
|
|
|
|
orig[key][0] = "foo"
|
|
|
|
if v := copy[key][0]; v != val {
|
|
|
|
t.Errorf("change in original should not affect copy, got %q, want %q", v, val)
|
2016-09-12 07:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:40:19 +03:00
|
|
|
func TestJoin(t *testing.T) {
|
2016-09-12 07:25:09 +03:00
|
|
|
for _, test := range []struct {
|
2017-10-05 17:40:19 +03:00
|
|
|
mds []MD
|
|
|
|
want MD
|
2016-09-12 07:25:09 +03:00
|
|
|
}{
|
2017-10-05 17:40:19 +03:00
|
|
|
{[]MD{}, MD{}},
|
|
|
|
{[]MD{Pairs("foo", "bar")}, Pairs("foo", "bar")},
|
|
|
|
{[]MD{Pairs("foo", "bar"), Pairs("foo", "baz")}, Pairs("foo", "bar", "foo", "baz")},
|
|
|
|
{[]MD{Pairs("foo", "bar"), Pairs("foo", "baz"), Pairs("zip", "zap")}, Pairs("foo", "bar", "foo", "baz", "zip", "zap")},
|
2016-09-12 07:25:09 +03:00
|
|
|
} {
|
2017-10-05 17:40:19 +03:00
|
|
|
md := Join(test.mds...)
|
|
|
|
if !reflect.DeepEqual(md, test.want) {
|
|
|
|
t.Errorf("context's metadata is %v, want %v", md, test.want)
|
2016-09-12 07:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|