From 62b58848d6dff10f84e44a2b4b18969cd1d19fcc Mon Sep 17 00:00:00 2001 From: Loner1024 Date: Thu, 7 Jul 2022 20:46:50 +0800 Subject: [PATCH] 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 --- metadata/metadata_test.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index b7f7173b0..8653bbe55 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -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{}) } }