1
2
3
4 package metautils_test
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
11 "github.com/stretchr/testify/assert"
12 "google.golang.org/grpc/metadata"
13 )
14
15 var (
16 testPairs = []string{"singlekey", "uno", "multikey", "one", "multikey", "two", "multikey", "three"}
17 parentCtx = context.WithValue(context.TODO(), "parentKey", "parentValue")
18 )
19
20 func assertRetainsParentContext(t *testing.T, ctx context.Context) {
21 x := ctx.Value("parentKey")
22 assert.EqualValues(t, "parentValue", x, "context must contain parentCtx")
23 }
24
25 func TestNiceMD_Get(t *testing.T) {
26 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
27 assert.Equal(t, "uno", nmd.Get("singlekey"), "for present single-key value it should return it")
28 assert.Equal(t, "one", nmd.Get("multikey"), "for present multi-key should return first value")
29 assert.Empty(t, nmd.Get("nokey"), "for non existing key should return stuff")
30 }
31
32 func TestNiceMD_Del(t *testing.T) {
33 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
34 assert.Equal(t, "uno", nmd.Get("singlekey"), "for present single-key value it should return it")
35 nmd.Del("singlekey").Del("doesnt exist")
36 assert.Empty(t, nmd.Get("singlekey"), "after deletion singlekey shouldn't exist")
37 }
38
39 func TestNiceMD_Add(t *testing.T) {
40 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
41 nmd.Add("multikey", "four").Add("newkey", "something")
42 assert.EqualValues(t, []string{"one", "two", "three", "four"}, nmd["multikey"], "append should add a new four at the end")
43 assert.EqualValues(t, []string{"something"}, nmd["newkey"], "append should be able to create new keys")
44 }
45
46 func TestNiceMD_Set(t *testing.T) {
47 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
48 nmd.Set("multikey", "one").Set("newkey", "something").Set("newkey", "another")
49 assert.EqualValues(t, []string{"one"}, nmd["multikey"], "set should override existing multi keys")
50 assert.EqualValues(t, []string{"another"}, nmd["newkey"], "set should override new keys")
51 }
52
53 func TestNiceMD_SetGet(t *testing.T) {
54 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
55 nmd.Set("another-key", "onetwothree")
56 assert.EqualValues(t, "onetwothree", nmd.Get("another-key"))
57 nmd.Set("another-key-bin", "binarydata")
58 assert.EqualValues(t, "binarydata", nmd.Get("another-key-bin"))
59 }
60
61 func TestNiceMD_Clone(t *testing.T) {
62 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
63 fullCopied := nmd.Clone()
64 assert.Equal(t, len(fullCopied), len(nmd), "clone full should copy all keys")
65 assert.Equal(t, "uno", fullCopied.Get("singlekey"), "full copied should have content")
66 subCopied := nmd.Clone("multikey")
67 assert.Len(t, subCopied, 1, "sub copied clone should only have one key")
68 assert.Empty(t, subCopied.Get("singlekey"), "there shouldn't be a singlekey in the subcopied")
69
70
71 assert.EqualValues(t, subCopied["multikey"], nmd["multikey"], "before overwrites multikey should have the same values")
72 subCopied["multikey"][1] = "modifiedtwo"
73 assert.NotEqual(t, subCopied["multikey"], nmd["multikey"], "before overwrites multikey should have the same values")
74 }
75
76 func TestNiceMD_ToOutgoing(t *testing.T) {
77 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
78 nCtx := nmd.ToOutgoing(parentCtx)
79 assertRetainsParentContext(t, nCtx)
80
81 eCtx := metautils.ExtractOutgoing(nCtx).Clone().Set("newvalue", "something").ToOutgoing(nCtx)
82 assertRetainsParentContext(t, eCtx)
83 assert.NotEqual(t, metautils.ExtractOutgoing(nCtx), metautils.ExtractOutgoing(eCtx), "the niceMD pointed to by ectx and nctx are different.")
84 }
85
86 func TestNiceMD_ToIncoming(t *testing.T) {
87 nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
88 nCtx := nmd.ToIncoming(parentCtx)
89 assertRetainsParentContext(t, nCtx)
90
91 eCtx := metautils.ExtractIncoming(nCtx).Clone().Set("newvalue", "something").ToIncoming(nCtx)
92 assertRetainsParentContext(t, eCtx)
93 assert.NotEqual(t, metautils.ExtractIncoming(nCtx), metautils.ExtractIncoming(eCtx), "the niceMD pointed to by ectx and nctx are different.")
94 }
95
View as plain text