gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/metadata/metadata_test.go (about) 1 /* 2 * 3 * Copyright 2020 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package metadata 20 21 import ( 22 "testing" 23 24 "gitee.com/ks-custle/core-gm/grpc/attributes" 25 "gitee.com/ks-custle/core-gm/grpc/metadata" 26 "gitee.com/ks-custle/core-gm/grpc/resolver" 27 "github.com/google/go-cmp/cmp" 28 ) 29 30 func TestGet(t *testing.T) { 31 tests := []struct { 32 name string 33 addr resolver.Address 34 want metadata.MD 35 }{ 36 { 37 name: "not set", 38 addr: resolver.Address{}, 39 want: nil, 40 }, 41 { 42 name: "not set", 43 addr: resolver.Address{ 44 Attributes: attributes.New(mdKey, mdValue(metadata.Pairs("k", "v"))), 45 }, 46 want: metadata.Pairs("k", "v"), 47 }, 48 } 49 for _, tt := range tests { 50 t.Run(tt.name, func(t *testing.T) { 51 if got := Get(tt.addr); !cmp.Equal(got, tt.want) { 52 t.Errorf("Get() = %v, want %v", got, tt.want) 53 } 54 }) 55 } 56 } 57 58 func TestSet(t *testing.T) { 59 tests := []struct { 60 name string 61 addr resolver.Address 62 md metadata.MD 63 }{ 64 { 65 name: "unset before", 66 addr: resolver.Address{}, 67 md: metadata.Pairs("k", "v"), 68 }, 69 { 70 name: "set before", 71 addr: resolver.Address{ 72 Attributes: attributes.New(mdKey, mdValue(metadata.Pairs("bef", "ore"))), 73 }, 74 md: metadata.Pairs("k", "v"), 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 newAddr := Set(tt.addr, tt.md) 80 newMD := Get(newAddr) 81 if !cmp.Equal(newMD, tt.md) { 82 t.Errorf("md after Set() = %v, want %v", newMD, tt.md) 83 } 84 }) 85 } 86 }