2023-07-29 00:40:58 +03:00
|
|
|
package options_test
|
|
|
|
|
|
|
|
import (
|
2024-02-28 15:06:29 +03:00
|
|
|
"fmt"
|
2023-07-29 00:40:58 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
2024-02-28 15:06:29 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2024-02-28 15:06:29 +03:00
|
|
|
"go.unistack.org/micro/v4/util/reflect"
|
2023-07-29 00:40:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddress(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
type s struct {
|
|
|
|
Address []string
|
|
|
|
}
|
|
|
|
|
|
|
|
src := &s{}
|
|
|
|
var opts []options.Option
|
|
|
|
opts = append(opts, options.Address("host:port"))
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
if err = o(src); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if src.Address[0] != "host:port" {
|
|
|
|
t.Fatal("failed to set Address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCodecs(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
type s struct {
|
|
|
|
Codecs map[string]codec.Codec
|
|
|
|
}
|
|
|
|
|
|
|
|
src := &s{}
|
|
|
|
var opts []options.Option
|
|
|
|
c := codec.NewCodec()
|
|
|
|
opts = append(opts, options.Codecs("text/html", c))
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
if err = o(src); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range src.Codecs {
|
|
|
|
if k != "text/html" || v != c {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("failed to set Codecs")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabels(t *testing.T) {
|
|
|
|
type str1 struct {
|
|
|
|
Labels []string
|
|
|
|
}
|
|
|
|
type str2 struct {
|
|
|
|
Labels []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
x1 := &str1{}
|
|
|
|
|
|
|
|
if err := options.Labels("one", "two")(x1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(x1.Labels) != 2 {
|
|
|
|
t.Fatal("failed to set labels")
|
|
|
|
}
|
|
|
|
x2 := &str2{}
|
|
|
|
if err := options.Labels("key", "val")(x2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(x2.Labels) != 2 {
|
|
|
|
t.Fatal("failed to set labels")
|
|
|
|
}
|
|
|
|
if x2.Labels[0] != "key" {
|
|
|
|
t.Fatal("failed to set labels")
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 15:06:29 +03:00
|
|
|
|
|
|
|
func TestMetadataAny(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
Metadata metadata.Metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Data any
|
|
|
|
Expected metadata.Metadata
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"strings_even",
|
2024-03-09 17:21:20 +03:00
|
|
|
[]any{"Strkey1", []string{"val1"}, "Strkey2", []string{"val2"}},
|
|
|
|
metadata.Pairs("Strkey1", "val1", "Strkey2", "val2"),
|
2024-02-28 15:06:29 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"strings_odd",
|
2024-02-29 01:27:33 +03:00
|
|
|
[]any{"key1", "val1", "key2"},
|
2024-03-09 17:21:20 +03:00
|
|
|
metadata.Pairs("Key1", "val1"),
|
2024-02-28 15:06:29 +03:00
|
|
|
},
|
|
|
|
{
|
2024-02-29 01:27:33 +03:00
|
|
|
Name: "map",
|
2024-03-09 17:21:20 +03:00
|
|
|
Data: map[string][]string{
|
|
|
|
"Mapkey1": {"val1"},
|
|
|
|
"Mapkey2": {"val2"},
|
2024-02-28 15:06:29 +03:00
|
|
|
},
|
2024-02-29 01:27:33 +03:00
|
|
|
Expected: metadata.Metadata{
|
2024-03-09 17:21:20 +03:00
|
|
|
"Mapkey1": []string{"val1"},
|
|
|
|
"Mapkey2": []string{"val2"},
|
2024-02-28 15:06:29 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"metadata.Metadata",
|
2024-03-09 17:21:20 +03:00
|
|
|
metadata.Pairs("key1", "val1", "key2", "val2"),
|
|
|
|
metadata.Pairs("Key1", "val1", "Key2", "val2"),
|
2024-02-28 15:06:29 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.Name, func(t *testing.T) {
|
|
|
|
src := &s{}
|
|
|
|
var opts []options.Option
|
2024-02-29 01:27:33 +03:00
|
|
|
switch valData := tt.Data.(type) {
|
|
|
|
case []any:
|
2024-03-09 17:21:20 +03:00
|
|
|
fmt.Printf("%s any %#+v\n", tt.Name, valData)
|
2024-02-29 01:27:33 +03:00
|
|
|
opts = append(opts, options.Metadata(valData...))
|
2024-03-09 17:21:20 +03:00
|
|
|
case map[string]string, map[string][]string, metadata.Metadata:
|
2024-02-29 01:27:33 +03:00
|
|
|
opts = append(opts, options.Metadata(valData))
|
|
|
|
}
|
|
|
|
|
2024-02-28 15:06:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
if err := o(src); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.Equal(tt.Expected, src.Metadata) {
|
2024-03-09 17:21:20 +03:00
|
|
|
t.Fatalf("expected: %v, actual: %v", tt.Expected, src.Metadata)
|
2024-02-28 15:06:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|