Skip to content

Commit c6735a5

Browse files
committed
grpcproxy: add unit test for with client auth token
Signed-off-by: Kristoffer Johansson <[email protected]>
1 parent 78a893d commit c6735a5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

server/proxy/grpcproxy/util_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 The etcd Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package grpcproxy
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"google.golang.org/grpc/metadata"
23+
24+
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
25+
)
26+
27+
func TestWithClientAuthToken(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
initContext func() context.Context
31+
want string
32+
}{
33+
{
34+
name: "with valid token",
35+
initContext: func() context.Context {
36+
md := metadata.Pairs(rpctypes.TokenFieldNameGRPC, "test-token-123")
37+
return metadata.NewIncomingContext(t.Context(), md)
38+
},
39+
want: "test-token-123",
40+
},
41+
{
42+
name: "without token",
43+
initContext: func() context.Context {
44+
return t.Context()
45+
},
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
ctx := t.Context()
52+
ctxWithToken := tt.initContext()
53+
54+
got := withClientAuthToken(ctx, ctxWithToken)
55+
56+
if tt.want == "" {
57+
assert.Equal(t, ctx, got)
58+
return
59+
}
60+
61+
md, ok := metadata.FromOutgoingContext(got)
62+
assert.True(t, ok)
63+
tokens := md[rpctypes.TokenFieldNameGRPC]
64+
assert.Len(t, tokens, 1)
65+
assert.Equal(t, tt.want, tokens[0])
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)