github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/metadata/metadata_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package metadata 18 19 import ( 20 "testing" 21 22 "golang.org/x/net/context" 23 24 "github.com/cloudwego/kitex/internal/test" 25 ) 26 27 func TestDecodeKeyValue(t *testing.T) { 28 key1, value1 := "k1", "v1" 29 k, v, err := DecodeKeyValue(key1, value1) 30 test.Assert(t, k == key1) 31 test.Assert(t, v == value1) 32 test.Assert(t, err == nil, err) 33 } 34 35 func TestPairs(t *testing.T) { 36 key1, value1 := "k1", "v1" 37 // new pairs 38 md := Pairs(key1, value1) 39 40 v1 := md.Get(key1)[0] 41 test.Assert(t, v1 == value1) 42 } 43 44 func TestMetadata(t *testing.T) { 45 key1, key2, key3 := "k1", "k2", "k3" 46 value1, value2, value3 := "v1", "v2", "v3" 47 value11 := "v11" 48 value2m := "v2m" 49 m := map[string]string{} 50 m[key1] = value1 51 m[key2] = value2 52 m[key3] = value3 53 md := New(m) 54 55 // test Len() 56 test.Assert(t, md.Len() == len(m)) 57 // test Get() 58 test.Assert(t, md.Get(key1)[0] == value1) 59 // test Append() 60 md.Append(key1) 61 test.Assert(t, len(md.Get(key1)) == 1) 62 md.Append(key1, value11) 63 test.Assert(t, md.Get(key1)[1] == value11) 64 // test Set() 65 md.Set(key2) 66 test.Assert(t, md.Get(key2)[0] == value2) 67 md.Set(key2, value2m) 68 test.Assert(t, md.Get(key2)[0] == value2m) 69 // test Copy() 70 copiedMd := md.Copy() 71 test.Assert(t, copiedMd.Get(key3)[0] == value3) 72 } 73 74 func TestContext(t *testing.T) { 75 key1, value1 := "k1", "v1" 76 md := Pairs(key1, value1) 77 78 // test incomingCtx 79 incomingCtx := NewIncomingContext(context.Background(), md) 80 _, incomingOk := FromIncomingContext(incomingCtx) 81 test.Assert(t, incomingOk) 82 83 // test outgoingCtx 84 outgoingCtx := NewOutgoingContext(context.Background(), md) 85 _, outgoingOk := FromOutgoingContext(outgoingCtx) 86 test.Assert(t, outgoingOk) 87 88 key2, key3, value2, value3 := "k2", "k3", "v2", "v3" 89 // test outgoingCtxRaw 90 AppendToOutgoingContext(outgoingCtx, key2, value2, key3, value3) 91 _, _, outgoingRawOk := FromOutgoingContextRaw(outgoingCtx) 92 test.Assert(t, outgoingRawOk) 93 } 94 95 func TestAppendMD(t *testing.T) { 96 t.Run("empty-md", func(t *testing.T) { 97 md := AppendMD(nil, MD{"k1": {"v1"}}) 98 test.Assert(t, md.Get("k1")[0] == "v1") 99 }) 100 t.Run("empty-other", func(t *testing.T) { 101 md := AppendMD(MD{"k1": {"v1"}}, nil) 102 test.Assert(t, md.Get("k1")[0] == "v1") 103 }) 104 t.Run("no-same-key", func(t *testing.T) { 105 md := AppendMD(MD{"k1": {"v1"}, "k3": {"v3"}}, MD{"k2": {"v2"}}) 106 test.Assert(t, md.Get("k1")[0] == "v1") 107 test.Assert(t, md.Get("k2")[0] == "v2") 108 test.Assert(t, md.Get("k3")[0] == "v3") 109 }) 110 t.Run("same-key", func(t *testing.T) { 111 md := AppendMD(MD{"k1": {"v1"}}, MD{"k1": {"v2"}, "k2": {"v3"}}) 112 test.Assert(t, md.Get("k1")[0] == "v1") 113 test.Assert(t, md.Get("k1")[1] == "v2") 114 test.Assert(t, md.Get("k2")[0] == "v3") 115 }) 116 }