File: metadata_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-middleware 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,464 kB
  • sloc: makefile: 107; sh: 9
file content (88 lines) | stat: -rw-r--r-- 3,781 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package metadata_test

import (
	"context"
	"testing"

	"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
	"github.com/stretchr/testify/assert"
	grpcMetadata "google.golang.org/grpc/metadata"
)

var parentKey struct{}

var (
	testPairs = []string{"singlekey", "uno", "multikey", "one", "multikey", "two", "multikey", "three"}
	parentCtx = context.WithValue(context.TODO(), parentKey, "parentValue")
)

func assertRetainsParentContext(t *testing.T, ctx context.Context) {
	x := ctx.Value(parentKey)
	assert.EqualValues(t, "parentValue", x, "context must contain parentCtx")
}

func TestNiceMD_Get(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	assert.Equal(t, "uno", md.Get("singlekey"), "for present single-key value it should return it")
	assert.Equal(t, "one", md.Get("multikey"), "for present multi-key should return first value")
	assert.Empty(t, md.Get("nokey"), "for non existing key should return stuff")
}

func TestNiceMD_Del(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	assert.Equal(t, "uno", md.Get("singlekey"), "for present single-key value it should return it")
	md.Del("singlekey").Del("doesnt exist")
	assert.Empty(t, md.Get("singlekey"), "after deletion singlekey shouldn't exist")
}

func TestNiceMD_Add(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	md.Add("multikey", "four").Add("newkey", "something")
	assert.EqualValues(t, []string{"one", "two", "three", "four"}, md["multikey"], "append should add a new four at the end")
	assert.EqualValues(t, []string{"something"}, md["newkey"], "append should be able to create new keys")
}

func TestNiceMD_Set(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	md.Set("multikey", "one").Set("newkey", "something").Set("newkey", "another")
	assert.EqualValues(t, []string{"one"}, md["multikey"], "set should override existing multi keys")
	assert.EqualValues(t, []string{"another"}, md["newkey"], "set should override new keys")
}

func TestNiceMD_Clone(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	fullCopied := md.Clone()
	assert.Equal(t, len(fullCopied), len(md), "clone full should copy all keys")
	assert.Equal(t, "uno", fullCopied.Get("singlekey"), "full copied should have content")
	subCopied := md.Clone("multikey")
	assert.Len(t, subCopied, 1, "sub copied clone should only have one key")
	assert.Empty(t, subCopied.Get("singlekey"), "there shouldn't be a singlekey in the subcopied")

	// Test side effects and full copying:
	assert.EqualValues(t, subCopied["multikey"], md["multikey"], "before overwrites multikey should have the same values")
	subCopied["multikey"][1] = "modifiedtwo"
	assert.NotEqual(t, subCopied["multikey"], md["multikey"], "before overwrites multikey should have the same values")
}

func TestNiceMD_ToOutgoing(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	nCtx := md.ToOutgoing(parentCtx)
	assertRetainsParentContext(t, nCtx)

	eCtx := metadata.ExtractOutgoing(nCtx).Clone().Set("newvalue", "something").ToOutgoing(nCtx)
	assertRetainsParentContext(t, eCtx)
	assert.NotEqual(t, metadata.ExtractOutgoing(nCtx), metadata.ExtractOutgoing(eCtx), "the niceMD pointed to by ectx and nctx are different.")
}

func TestNiceMD_ToIncoming(t *testing.T) {
	md := metadata.MD(grpcMetadata.Pairs(testPairs...))
	nCtx := md.ToIncoming(parentCtx)
	assertRetainsParentContext(t, nCtx)

	eCtx := metadata.ExtractIncoming(nCtx).Clone().Set("newvalue", "something").ToIncoming(nCtx)
	assertRetainsParentContext(t, eCtx)
	assert.NotEqual(t, metadata.ExtractIncoming(nCtx), metadata.ExtractIncoming(eCtx), "the niceMD pointed to by ectx and nctx are different.")
}