forked from streamingfast/dauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers_test.go
49 lines (35 loc) · 1014 Bytes
/
headers_test.go
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
package dauth
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
)
func TestHeadersContext(t *testing.T) {
ctx := context.Background()
th := make(TrustedHeaders)
th[SFHeaderUserID] = "my-user-id"
th["random-key"] = "102"
ctx = WithTrustedHeaders(ctx, th)
got := FromContext(ctx)
assert.Equal(t, "my-user-id", got.Get(SFHeaderUserID))
assert.Equal(t, "102", got.Get("random-key"))
}
func TestHeadersMetadataContext(t *testing.T) {
th := make(TrustedHeaders)
th[SFHeaderUserID] = "my-user-id"
th[SFHeaderIP] = "10.2.3.4"
ctx := context.Background()
ctx = th.ToOutgoingGRPCContext(ctx)
md, ok := metadata.FromOutgoingContext(ctx)
assert.True(t, ok)
assert.Equal(t, []string{"my-user-id"}, md.Get(SFHeaderUserID))
}
func TestNilDoesntPanic(t *testing.T) {
ctx := context.Background()
h := FromContext(ctx)
assert.Nil(t, h)
assert.Equal(t, "", h.RealIP())
assert.Equal(t, "", h.UserID())
assert.Equal(t, "", h.Get("something"))
}