|
|
|
@ -8,7 +8,7 @@ import ( |
|
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
mds []map[string]string |
|
|
|
|
mds []map[string][]string |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
@ -17,13 +17,13 @@ func TestNew(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "hello", |
|
|
|
|
args: args{[]map[string]string{{"hello": "kratos"}, {"hello2": "go-kratos"}}}, |
|
|
|
|
want: Metadata{"hello": "kratos", "hello2": "go-kratos"}, |
|
|
|
|
args: args{[]map[string][]string{{"hello": {"kratos"}}, {"hello2": {"go-kratos"}}}}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "hello2": {"go-kratos"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "hi", |
|
|
|
|
args: args{[]map[string]string{{"hi": "kratos"}, {"hi2": "go-kratos"}}}, |
|
|
|
|
want: Metadata{"hi": "kratos", "hi2": "go-kratos"}, |
|
|
|
|
args: args{[]map[string][]string{{"hi": {"kratos"}}, {"hi2": {"go-kratos"}}}}, |
|
|
|
|
want: Metadata{"hi": {"kratos"}, "hi2": {"go-kratos"}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -47,13 +47,13 @@ func TestMetadata_Get(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
m: Metadata{"kratos": "value", "env": "dev"}, |
|
|
|
|
m: Metadata{"kratos": {"value"}, "env": {"dev"}}, |
|
|
|
|
args: args{key: "kratos"}, |
|
|
|
|
want: "value", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "env", |
|
|
|
|
m: Metadata{"kratos": "value", "env": "dev"}, |
|
|
|
|
m: Metadata{"kratos": {"value"}, "env": {"dev"}}, |
|
|
|
|
args: args{key: "env"}, |
|
|
|
|
want: "dev", |
|
|
|
|
}, |
|
|
|
@ -67,6 +67,38 @@ func TestMetadata_Get(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMetadata_Values(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
key string |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
m Metadata |
|
|
|
|
args args |
|
|
|
|
want []string |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
m: Metadata{"kratos": {"value", "value2"}, "env": {"dev"}}, |
|
|
|
|
args: args{key: "kratos"}, |
|
|
|
|
want: []string{"value", "value2"}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "env", |
|
|
|
|
m: Metadata{"kratos": {"value", "value2"}, "env": {"dev"}}, |
|
|
|
|
args: args{key: "env"}, |
|
|
|
|
want: []string{"dev"}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
if got := tt.m.Values(tt.args.key); !reflect.DeepEqual(got, tt.want) { |
|
|
|
|
t.Errorf("Get() = %v, want %v", got, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMetadata_Set(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
key string |
|
|
|
@ -82,13 +114,13 @@ func TestMetadata_Set(t *testing.T) { |
|
|
|
|
name: "kratos", |
|
|
|
|
m: Metadata{}, |
|
|
|
|
args: args{key: "hello", value: "kratos"}, |
|
|
|
|
want: Metadata{"hello": "kratos"}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "env", |
|
|
|
|
m: Metadata{"hello": "kratos"}, |
|
|
|
|
m: Metadata{"hello": {"kratos"}}, |
|
|
|
|
args: args{key: "env", value: "pro"}, |
|
|
|
|
want: Metadata{"hello": "kratos", "env": "pro"}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "env": {"pro"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "empty", |
|
|
|
@ -107,6 +139,46 @@ func TestMetadata_Set(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMetadata_Add(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
key string |
|
|
|
|
value string |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
m Metadata |
|
|
|
|
args args |
|
|
|
|
want Metadata |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
m: Metadata{}, |
|
|
|
|
args: args{key: "hello", value: "kratos"}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "env", |
|
|
|
|
m: Metadata{"hello": {"kratos"}}, |
|
|
|
|
args: args{key: "hello", value: "again"}, |
|
|
|
|
want: Metadata{"hello": {"kratos", "again"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "empty", |
|
|
|
|
m: Metadata{}, |
|
|
|
|
args: args{key: "", value: ""}, |
|
|
|
|
want: Metadata{}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
tt.m.Add(tt.args.key, tt.args.value) |
|
|
|
|
if !reflect.DeepEqual(tt.m, tt.want) { |
|
|
|
|
t.Errorf("Set() = %v, want %v", tt.m, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestClientContext(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
ctx context.Context |
|
|
|
@ -118,11 +190,11 @@ func TestClientContext(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, |
|
|
|
|
args: args{context.Background(), Metadata{"hello": {"kratos"}, "kratos": {"https://go-kratos.dev"}}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "hello", |
|
|
|
|
args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, |
|
|
|
|
args: args{context.Background(), Metadata{"hello": {"kratos"}, "hello2": {"https://go-kratos.dev"}}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -151,11 +223,11 @@ func TestServerContext(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, |
|
|
|
|
args: args{context.Background(), Metadata{"hello": {"kratos"}, "kratos": {"https://go-kratos.dev"}}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "hello", |
|
|
|
|
args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, |
|
|
|
|
args: args{context.Background(), Metadata{"hello": {"kratos"}, "hello2": {"https://go-kratos.dev"}}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -186,12 +258,12 @@ func TestAppendToClientContext(t *testing.T) { |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
args: args{Metadata{}, []string{"hello", "kratos", "env", "dev"}}, |
|
|
|
|
want: Metadata{"hello": "kratos", "env": "dev"}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "env": {"dev"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "hello", |
|
|
|
|
args: args{Metadata{"hi": "https://go-kratos.dev/"}, []string{"hello", "kratos", "env", "dev"}}, |
|
|
|
|
want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, |
|
|
|
|
args: args{Metadata{"hi": {"https://go-kratos.dev/"}}, []string{"hello", "kratos", "env", "dev"}}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "env": {"dev"}, "hi": {"https://go-kratos.dev/"}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -240,13 +312,13 @@ func TestMergeToClientContext(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
args: args{Metadata{}, Metadata{"hello": "kratos", "env": "dev"}}, |
|
|
|
|
want: Metadata{"hello": "kratos", "env": "dev"}, |
|
|
|
|
args: args{Metadata{}, Metadata{"hello": {"kratos"}, "env": {"dev"}}}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "env": {"dev"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "hello", |
|
|
|
|
args: args{Metadata{"hi": "https://go-kratos.dev/"}, Metadata{"hello": "kratos", "env": "dev"}}, |
|
|
|
|
want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, |
|
|
|
|
args: args{Metadata{"hi": {"https://go-kratos.dev/"}}, Metadata{"hello": {"kratos"}, "env": {"dev"}}}, |
|
|
|
|
want: Metadata{"hello": {"kratos"}, "env": {"dev"}, "hi": {"https://go-kratos.dev/"}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -265,19 +337,19 @@ func TestMergeToClientContext(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMetadata_Range(t *testing.T) { |
|
|
|
|
md := Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"} |
|
|
|
|
md := Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}} |
|
|
|
|
tmp := Metadata{} |
|
|
|
|
md.Range(func(k, v string) bool { |
|
|
|
|
md.Range(func(k string, v []string) bool { |
|
|
|
|
if k == "https://go-kratos.dev/" || k == "kratos" { |
|
|
|
|
tmp[k] = v |
|
|
|
|
} |
|
|
|
|
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{"https://go-kratos.dev/": "https://go-kratos.dev/", "kratos": "kratos"}) |
|
|
|
|
if !reflect.DeepEqual(tmp, Metadata{"https://go-kratos.dev/": {"https://go-kratos.dev/"}, "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 { |
|
|
|
|
md.Range(func(k string, v []string) bool { |
|
|
|
|
return false |
|
|
|
|
}) |
|
|
|
|
if !reflect.DeepEqual(tmp, Metadata{}) { |
|
|
|
@ -293,13 +365,13 @@ func TestMetadata_Clone(t *testing.T) { |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "kratos", |
|
|
|
|
m: Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"}, |
|
|
|
|
want: Metadata{"kratos": "kratos", "https://go-kratos.dev/": "https://go-kratos.dev/", "go-kratos": "go-kratos"}, |
|
|
|
|
m: Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}}, |
|
|
|
|
want: Metadata{"kratos": {"kratos"}, "https://go-kratos.dev/": {"https://go-kratos.dev/"}, "go-kratos": {"go-kratos"}}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "go", |
|
|
|
|
m: Metadata{"language": "golang"}, |
|
|
|
|
want: Metadata{"language": "golang"}, |
|
|
|
|
m: Metadata{"language": {"golang"}}, |
|
|
|
|
want: Metadata{"language": {"golang"}}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
@ -308,7 +380,7 @@ func TestMetadata_Clone(t *testing.T) { |
|
|
|
|
if !reflect.DeepEqual(got, tt.want) { |
|
|
|
|
t.Errorf("Clone() = %v, want %v", got, tt.want) |
|
|
|
|
} |
|
|
|
|
got["kratos"] = "go" |
|
|
|
|
got["kratos"] = []string{"go"} |
|
|
|
|
if reflect.DeepEqual(got, tt.want) { |
|
|
|
|
t.Errorf("want got != want got %v want %v", got, tt.want) |
|
|
|
|
} |
|
|
|
|