9 "google.golang.org/grpc/metadata"
12 func TestHTTPToContext(t *testing.T) {
13 reqFunc := HTTPToContext()
15 // When the header doesn't exist
16 ctx := reqFunc(context.Background(), &http.Request{})
18 if ctx.Value(JWTTokenContextKey) != nil {
19 t.Error("Context shouldn't contain the encoded JWT")
22 // Authorization header value has invalid format
23 header := http.Header{}
24 header.Set("Authorization", "no expected auth header format value")
25 ctx = reqFunc(context.Background(), &http.Request{Header: header})
27 if ctx.Value(JWTTokenContextKey) != nil {
28 t.Error("Context shouldn't contain the encoded JWT")
31 // Authorization header is correct
32 header.Set("Authorization", generateAuthHeaderFromToken(signedKey))
33 ctx = reqFunc(context.Background(), &http.Request{Header: header})
35 token := ctx.Value(JWTTokenContextKey).(string)
36 if token != signedKey {
37 t.Errorf("Context doesn't contain the expected encoded token value; expected: %s, got: %s", signedKey, token)
41 func TestContextToHTTP(t *testing.T) {
42 reqFunc := ContextToHTTP()
44 // No JWT Token is passed in the context
45 ctx := context.Background()
49 token := r.Header.Get("Authorization")
51 t.Error("authorization key should not exist in metadata")
54 // Correct JWT Token is passed in the context
55 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
56 r = http.Request{Header: http.Header{}}
59 token = r.Header.Get("Authorization")
60 expected := generateAuthHeaderFromToken(signedKey)
62 if token != expected {
63 t.Errorf("Authorization header does not contain the expected JWT token; expected %s, got %s", expected, token)
67 func TestGRPCToContext(t *testing.T) {
69 reqFunc := GRPCToContext()
71 // No Authorization header is passed
72 ctx := reqFunc(context.Background(), md)
73 token := ctx.Value(JWTTokenContextKey)
75 t.Error("Context should not contain a JWT Token")
78 // Invalid Authorization header is passed
79 md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
80 ctx = reqFunc(context.Background(), md)
81 token = ctx.Value(JWTTokenContextKey)
83 t.Error("Context should not contain a JWT Token")
86 // Authorization header is correct
87 md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
88 ctx = reqFunc(context.Background(), md)
89 token, ok := ctx.Value(JWTTokenContextKey).(string)
91 t.Fatal("JWT Token not passed to context correctly")
94 if token != signedKey {
95 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token)
99 func TestContextToGRPC(t *testing.T) {
100 reqFunc := ContextToGRPC()
102 // No JWT Token is passed in the context
103 ctx := context.Background()
107 _, ok := md["authorization"]
109 t.Error("authorization key should not exist in metadata")
112 // Correct JWT Token is passed in the context
113 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
117 token, ok := md["authorization"]
119 t.Fatal("JWT Token not passed to metadata correctly")
122 if token[0] != generateAuthHeaderFromToken(signedKey) {
123 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])