github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/util/metautils/nicemd_test.go (about)

     1  // Copyright 2016 Michal Witkowski. All Rights Reserved.
     2  // See LICENSE for licensing terms.
     3  
     4  package metautils_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hxx258456/ccgo/go-grpc-middleware/util/metautils"
    10  	"github.com/hxx258456/ccgo/grpc/metadata"
    11  	"github.com/hxx258456/ccgo/net/context"
    12  	"github.com/stretchr/testify/assert"
    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_Clone(t *testing.T) {
    54  	nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
    55  	fullCopied := nmd.Clone()
    56  	assert.Equal(t, len(fullCopied), len(nmd), "clone full should copy all keys")
    57  	assert.Equal(t, "uno", fullCopied.Get("singlekey"), "full copied should have content")
    58  	subCopied := nmd.Clone("multikey")
    59  	assert.Len(t, subCopied, 1, "sub copied clone should only have one key")
    60  	assert.Empty(t, subCopied.Get("singlekey"), "there shouldn't be a singlekey in the subcopied")
    61  
    62  	// Test side effects and full copying:
    63  	assert.EqualValues(t, subCopied["multikey"], nmd["multikey"], "before overwrites multikey should have the same values")
    64  	subCopied["multikey"][1] = "modifiedtwo"
    65  	assert.NotEqual(t, subCopied["multikey"], nmd["multikey"], "before overwrites multikey should have the same values")
    66  }
    67  
    68  func TestNiceMD_ToOutgoing(t *testing.T) {
    69  	nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
    70  	nCtx := nmd.ToOutgoing(parentCtx)
    71  	assertRetainsParentContext(t, nCtx)
    72  
    73  	eCtx := metautils.ExtractOutgoing(nCtx).Clone().Set("newvalue", "something").ToOutgoing(nCtx)
    74  	assertRetainsParentContext(t, eCtx)
    75  	assert.NotEqual(t, metautils.ExtractOutgoing(nCtx), metautils.ExtractOutgoing(eCtx), "the niceMD pointed to by ectx and nctx are different.")
    76  }
    77  
    78  func TestNiceMD_ToIncoming(t *testing.T) {
    79  	nmd := metautils.NiceMD(metadata.Pairs(testPairs...))
    80  	nCtx := nmd.ToIncoming(parentCtx)
    81  	assertRetainsParentContext(t, nCtx)
    82  
    83  	eCtx := metautils.ExtractIncoming(nCtx).Clone().Set("newvalue", "something").ToIncoming(nCtx)
    84  	assertRetainsParentContext(t, eCtx)
    85  	assert.NotEqual(t, metautils.ExtractIncoming(nCtx), metautils.ExtractIncoming(eCtx), "the niceMD pointed to by ectx and nctx are different.")
    86  }