OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / auth / jwt / transport_test.go
1 package jwt
2
3 import (
4         "context"
5         "fmt"
6         "net/http"
7         "testing"
8
9         "google.golang.org/grpc/metadata"
10 )
11
12 func TestHTTPToContext(t *testing.T) {
13         reqFunc := HTTPToContext()
14
15         // When the header doesn't exist
16         ctx := reqFunc(context.Background(), &http.Request{})
17
18         if ctx.Value(JWTTokenContextKey) != nil {
19                 t.Error("Context shouldn't contain the encoded JWT")
20         }
21
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})
26
27         if ctx.Value(JWTTokenContextKey) != nil {
28                 t.Error("Context shouldn't contain the encoded JWT")
29         }
30
31         // Authorization header is correct
32         header.Set("Authorization", generateAuthHeaderFromToken(signedKey))
33         ctx = reqFunc(context.Background(), &http.Request{Header: header})
34
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)
38         }
39 }
40
41 func TestContextToHTTP(t *testing.T) {
42         reqFunc := ContextToHTTP()
43
44         // No JWT Token is passed in the context
45         ctx := context.Background()
46         r := http.Request{}
47         reqFunc(ctx, &r)
48
49         token := r.Header.Get("Authorization")
50         if token != "" {
51                 t.Error("authorization key should not exist in metadata")
52         }
53
54         // Correct JWT Token is passed in the context
55         ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
56         r = http.Request{Header: http.Header{}}
57         reqFunc(ctx, &r)
58
59         token = r.Header.Get("Authorization")
60         expected := generateAuthHeaderFromToken(signedKey)
61
62         if token != expected {
63                 t.Errorf("Authorization header does not contain the expected JWT token; expected %s, got %s", expected, token)
64         }
65 }
66
67 func TestGRPCToContext(t *testing.T) {
68         md := metadata.MD{}
69         reqFunc := GRPCToContext()
70
71         // No Authorization header is passed
72         ctx := reqFunc(context.Background(), md)
73         token := ctx.Value(JWTTokenContextKey)
74         if token != nil {
75                 t.Error("Context should not contain a JWT Token")
76         }
77
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)
82         if token != nil {
83                 t.Error("Context should not contain a JWT Token")
84         }
85
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)
90         if !ok {
91                 t.Fatal("JWT Token not passed to context correctly")
92         }
93
94         if token != signedKey {
95                 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token)
96         }
97 }
98
99 func TestContextToGRPC(t *testing.T) {
100         reqFunc := ContextToGRPC()
101
102         // No JWT Token is passed in the context
103         ctx := context.Background()
104         md := metadata.MD{}
105         reqFunc(ctx, &md)
106
107         _, ok := md["authorization"]
108         if ok {
109                 t.Error("authorization key should not exist in metadata")
110         }
111
112         // Correct JWT Token is passed in the context
113         ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
114         md = metadata.MD{}
115         reqFunc(ctx, &md)
116
117         token, ok := md["authorization"]
118         if !ok {
119                 t.Fatal("JWT Token not passed to metadata correctly")
120         }
121
122         if token[0] != generateAuthHeaderFromToken(signedKey) {
123                 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
124         }
125 }