mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
test: supplement the unit testing of metadata (#2161)
* test:supplement metadata set test * test:supplement metadata range test * test:supplement metadata append to client context test * test:fix linter error * text:fix metadate range test
This commit is contained in:
parent
b6954d1aeb
commit
62b58848d6
@ -90,6 +90,12 @@ func TestMetadata_Set(t *testing.T) {
|
||||
args: args{key: "env", value: "pro"},
|
||||
want: Metadata{"hello": "kratos", "env": "pro"},
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
m: Metadata{},
|
||||
args: args{key: "", value: ""},
|
||||
want: Metadata{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@ -203,6 +209,25 @@ func TestAppendToClientContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint directives: sa5012
|
||||
func TestAppendToClientContextThatPanics(t *testing.T) {
|
||||
kvs := []string{"hello", "kratos", "env"}
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf("append to client context singular kvs did not panic")
|
||||
}
|
||||
}()
|
||||
ctx := NewClientContext(context.Background(), Metadata{})
|
||||
ctx = AppendToClientContext(ctx, kvs...)
|
||||
md, ok := FromClientContext(ctx)
|
||||
if !ok {
|
||||
t.Errorf("FromServerContext() = %v, want %v", ok, true)
|
||||
}
|
||||
if !reflect.DeepEqual(md, Metadata{}) {
|
||||
t.Errorf("metadata = %v, want %v", md, Metadata{})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeToClientContext(t *testing.T) {
|
||||
type args struct {
|
||||
md Metadata
|
||||
@ -249,7 +274,14 @@ func TestMetadata_Range(t *testing.T) {
|
||||
return true
|
||||
})
|
||||
if !reflect.DeepEqual(tmp, Metadata{"https://go-kratos.dev/": "https://go-kratos.dev/", "kratos": "kratos"}) {
|
||||
t.Errorf("metadata = %v, want %v", tmp, Metadata{"kratos": "kratos"})
|
||||
t.Errorf("metadata = %v, want %v", tmp, Metadata{"https://go-kratos.dev/": "https://go-kratos.dev/", "kratos": "kratos"})
|
||||
}
|
||||
tmp = Metadata{}
|
||||
md.Range(func(k, v string) bool {
|
||||
return false
|
||||
})
|
||||
if !reflect.DeepEqual(tmp, Metadata{}) {
|
||||
t.Errorf("metadata = %v, want %v", tmp, Metadata{})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user