/
transport_test.go
125 lines (99 loc) · 3.31 KB
/
transport_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package jwt
import (
"context"
"fmt"
"net/http"
"testing"
"google.golang.org/grpc/metadata"
)
func TestToHTTPContext(t *testing.T) {
reqFunc := ToHTTPContext()
// When the header doesn't exist
ctx := reqFunc(context.Background(), &http.Request{})
if ctx.Value(JWTTokenContextKey) != nil {
t.Error("Context shouldn't contain the encoded JWT")
}
// Authorization header value has invalid format
header := http.Header{}
header.Set("Authorization", "no expected auth header format value")
ctx = reqFunc(context.Background(), &http.Request{Header: header})
if ctx.Value(JWTTokenContextKey) != nil {
t.Error("Context shouldn't contain the encoded JWT")
}
// Authorization header is correct
header.Set("Authorization", generateAuthHeaderFromToken(signedKey))
ctx = reqFunc(context.Background(), &http.Request{Header: header})
token := ctx.Value(JWTTokenContextKey).(string)
if token != signedKey {
t.Errorf("Context doesn't contain the expected encoded token value; expected: %s, got: %s", signedKey, token)
}
}
func TestFromHTTPContext(t *testing.T) {
reqFunc := FromHTTPContext()
// No JWT Token is passed in the context
ctx := context.Background()
r := http.Request{}
reqFunc(ctx, &r)
token := r.Header.Get("Authorization")
if token != "" {
t.Error("authorization key should not exist in metadata")
}
// Correct JWT Token is passed in the context
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
r = http.Request{Header: http.Header{}}
reqFunc(ctx, &r)
token = r.Header.Get("Authorization")
expected := generateAuthHeaderFromToken(signedKey)
if token != expected {
t.Errorf("Authorization header does not contain the expected JWT token; expected %s, got %s", expected, token)
}
}
func TestToGRPCContext(t *testing.T) {
md := metadata.MD{}
reqFunc := ToGRPCContext()
// No Authorization header is passed
ctx := reqFunc(context.Background(), &md)
token := ctx.Value(JWTTokenContextKey)
if token != nil {
t.Error("Context should not contain a JWT Token")
}
// Invalid Authorization header is passed
md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
ctx = reqFunc(context.Background(), &md)
token = ctx.Value(JWTTokenContextKey)
if token != nil {
t.Error("Context should not contain a JWT Token")
}
// Authorization header is correct
md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
ctx = reqFunc(context.Background(), &md)
token, ok := ctx.Value(JWTTokenContextKey).(string)
if !ok {
t.Fatal("JWT Token not passed to context correctly")
}
if token != signedKey {
t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token)
}
}
func TestFromGRPCContext(t *testing.T) {
reqFunc := FromGRPCContext()
// No JWT Token is passed in the context
ctx := context.Background()
md := metadata.MD{}
reqFunc(ctx, &md)
_, ok := md["authorization"]
if ok {
t.Error("authorization key should not exist in metadata")
}
// Correct JWT Token is passed in the context
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
md = metadata.MD{}
reqFunc(ctx, &md)
token, ok := md["authorization"]
if !ok {
t.Fatal("JWT Token not passed to metadata correctly")
}
if token[0] != generateAuthHeaderFromToken(signedKey) {
t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
}
}