2016-03-14 21:32:08 +03:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-03-14 21:32:08 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-05-03 15:19:10 +03:00
|
|
|
func TestMetadataSetMultiple(t *testing.T) {
|
|
|
|
md := New(4)
|
|
|
|
md.Set("key1", "val1", "key2", "val2", "key3")
|
|
|
|
|
|
|
|
if v, ok := md.Get("key1"); !ok || v != "val1" {
|
|
|
|
t.Fatalf("invalid kv %#+v", md)
|
|
|
|
}
|
|
|
|
if v, ok := md.Get("key2"); !ok || v != "val2" {
|
|
|
|
t.Fatalf("invalid kv %#+v", md)
|
|
|
|
}
|
|
|
|
if _, ok := md.Get("key3"); ok {
|
|
|
|
t.Fatalf("invalid kv %#+v", md)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:08:05 +03:00
|
|
|
func TestAppend(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = AppendIncomingContext(ctx, "key1", "val1", "key2", "val2")
|
|
|
|
md, ok := FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("metadata empty")
|
|
|
|
}
|
|
|
|
if _, ok := md.Get("key1"); !ok {
|
|
|
|
t.Fatal("key1 not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPairs(t *testing.T) {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs("key1", "val1", "key2", "val2")
|
|
|
|
|
|
|
|
if _, ok := md.Get("key1"); !ok {
|
2021-02-22 00:08:05 +03:00
|
|
|
t.Fatal("key1 not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 17:21:20 +03:00
|
|
|
func testIncomingCtx(ctx context.Context) {
|
|
|
|
if md, ok := FromIncomingContext(ctx); ok && md != nil {
|
|
|
|
md.Set("Key1", "Val1_new")
|
|
|
|
md.Set("Key3", "Val3")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOutgoingCtx(ctx context.Context) {
|
|
|
|
if md, ok := FromOutgoingContext(ctx); ok && md != nil {
|
|
|
|
md.Set("Key1", "Val1_new")
|
|
|
|
md.Set("Key3", "Val3")
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassing(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
md1 := New(2)
|
|
|
|
md1.Set("Key1", "Val1")
|
|
|
|
md1.Set("Key2", "Val2")
|
|
|
|
|
2024-03-09 17:21:20 +03:00
|
|
|
ctx = NewOutgoingContext(ctx, md1)
|
|
|
|
testOutgoingCtx(ctx)
|
2021-02-09 12:46:14 +03:00
|
|
|
md, ok := FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("missing metadata from outgoing context")
|
|
|
|
}
|
2021-02-12 17:10:35 +03:00
|
|
|
if v, ok := md.Get("Key1"); !ok || v != "Val1_new" {
|
|
|
|
t.Fatalf("invalid metadata value %#+v", md)
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 00:09:07 +03:00
|
|
|
func TestMerge(t *testing.T) {
|
|
|
|
omd := Metadata{
|
2024-03-09 17:21:20 +03:00
|
|
|
"key1": []string{"val1"},
|
2021-01-23 00:09:07 +03:00
|
|
|
}
|
|
|
|
mmd := Metadata{
|
2024-03-09 17:21:20 +03:00
|
|
|
"key2": []string{"val2"},
|
2021-01-23 00:09:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nmd := Merge(omd, mmd, true)
|
|
|
|
if len(nmd) != 2 {
|
|
|
|
t.Fatalf("merge failed: %v", nmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:17:59 +03:00
|
|
|
func TestMedataCanonicalKey(t *testing.T) {
|
2021-02-09 01:08:45 +03:00
|
|
|
md := New(1)
|
|
|
|
md.Set("x-request-id", "12345")
|
|
|
|
v, ok := md.Get("x-request-id")
|
2020-10-12 17:17:59 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed to get x-request-id")
|
|
|
|
} else if v != "12345" {
|
|
|
|
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
v, ok = md.Get("X-Request-Id")
|
2020-10-12 17:17:59 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed to get x-request-id")
|
|
|
|
} else if v != "12345" {
|
|
|
|
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
|
|
|
}
|
2021-02-09 01:08:45 +03:00
|
|
|
v, ok = md.Get("X-Request-ID")
|
2020-10-12 17:17:59 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("failed to get x-request-id")
|
|
|
|
} else if v != "12345" {
|
|
|
|
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:04:47 +03:00
|
|
|
func TestMetadataSet(t *testing.T) {
|
2021-02-09 01:08:45 +03:00
|
|
|
md := New(1)
|
2020-02-21 23:04:47 +03:00
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
md.Set("Key", "val")
|
|
|
|
|
|
|
|
val, ok := md.Get("Key")
|
2020-02-21 23:04:47 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("key Key not found")
|
|
|
|
}
|
|
|
|
if val != "val" {
|
|
|
|
t.Errorf("key Key with value val != %v", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:39:18 +03:00
|
|
|
func TestMetadataDelete(t *testing.T) {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs("Foo", "bar", "Baz", "empty")
|
2020-03-31 22:55:33 +03:00
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
md.Del("Baz")
|
|
|
|
_, ok := md.Get("Baz")
|
2020-03-31 22:55:33 +03:00
|
|
|
if ok {
|
|
|
|
t.Fatal("key Baz not deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:57:33 +03:00
|
|
|
func TestNilContext(t *testing.T) {
|
|
|
|
var ctx context.Context
|
|
|
|
|
|
|
|
_, ok := FromContext(ctx)
|
|
|
|
if ok {
|
|
|
|
t.Fatal("nil context")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 12:40:49 +03:00
|
|
|
func TestMetadataCopy(t *testing.T) {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs("Foo", "bar", "Bar", "baz")
|
2019-01-17 12:40:49 +03:00
|
|
|
|
|
|
|
cp := Copy(md)
|
|
|
|
|
|
|
|
for k, v := range md {
|
2024-03-09 17:21:20 +03:00
|
|
|
if cv := cp[k]; len(cv) != len(v) {
|
2019-01-17 12:40:49 +03:00
|
|
|
t.Fatalf("Got %s:%s for %s:%s", k, cv, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 21:32:08 +03:00
|
|
|
func TestMetadataContext(t *testing.T) {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs("Foo", "bar")
|
2016-03-14 21:32:08 +03:00
|
|
|
|
|
|
|
ctx := NewContext(context.TODO(), md)
|
|
|
|
|
|
|
|
emd, ok := FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected error retrieving metadata, got %t", ok)
|
|
|
|
}
|
|
|
|
|
2024-03-09 17:21:20 +03:00
|
|
|
if len(emd["Foo"]) != len(md["Foo"]) {
|
2019-12-31 16:37:29 +03:00
|
|
|
t.Errorf("Expected key: %s val: %s, got key: %s val: %s", "Foo", md["Foo"], "Foo", emd["Foo"])
|
2016-03-14 21:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if i := len(emd); i != 1 {
|
|
|
|
t.Errorf("Expected metadata length 1 got %d", i)
|
|
|
|
}
|
|
|
|
}
|