-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
executable file
·90 lines (79 loc) · 2.28 KB
/
header_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
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
89
90
package trace
import (
"testing"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)
func TestHeaderFromContext(t *testing.T) {
context := context.Background()
if header := HeaderFromContext(context); header == nil {
t.Log("header is nil.")
} else {
t.Error("HeaderFromContext return not correct.")
}
m := make(map[string]string)
md := metadata.New(m)
ctx := metadata.NewOutgoingContext(context, md)
ctx = toIncomingCtx(ctx)
if header := HeaderFromContext(ctx); header == nil {
t.Error("HeaderFromContext return not correct.")
} else {
t.Log(header)
}
m[XUid] = "uid"
m[XCid] = "cid"
m[XAid] = "aid"
m[XRequestId] = "requestid"
md = metadata.New(m)
ctx = metadata.NewOutgoingContext(ctx, md)
ctx = toIncomingCtx(ctx)
if header := HeaderFromContext(ctx); header == nil {
t.Error("HeaderFromContext return not correct.")
} else {
t.Log(header)
}
m[XSource] = "source"
m[XClient] = "client"
m[XAppVersion] = "appVersion"
m[XDid] = "did"
md = metadata.New(m)
ctx = metadata.NewOutgoingContext(ctx, md)
ctx = toIncomingCtx(ctx)
if header := HeaderFromContext(ctx); header == nil {
t.Error("HeaderFromContext return not correct.")
} else {
t.Log(header)
}
}
func TestTokenFromContext(t *testing.T) {
context := context.Background()
if token := TokenFromContext(context); token == "" {
t.Log("token is null.")
} else {
t.Error("TokenFromContext return not correct.")
}
m := make(map[string]string)
md := metadata.New(m)
ctx := metadata.NewOutgoingContext(context, md)
ctx = toIncomingCtx(ctx)
if token := TokenFromContext(ctx); token == "" {
t.Log("token is null.")
} else {
t.Error("TokenFromContext return not correct.")
}
m[Cookie] = "JINSESSIONID=c19f3976-ce6b-4746-b36a-f8b6774a8b39; aid=cHEIZrMbR2Ku2SfwbT9VOg; cid=2; uid=17198277"
md = metadata.New(m)
ctx = metadata.NewOutgoingContext(ctx, md)
ctx = toIncomingCtx(ctx)
if token := TokenFromContext(ctx); token != "c19f3976-ce6b-4746-b36a-f8b6774a8b39" {
t.Errorf("TokenFromContext return not correct.")
} else {
t.Log(token)
}
}
func toIncomingCtx(outgoing context.Context) context.Context {
if md, ok := metadata.FromOutgoingContext(outgoing); ok {
return metadata.NewIncomingContext(context.Background(), md)
}
return metadata.NewIncomingContext(context.Background(), nil)
}