Codebase list golang-github-go-kit-kit / eb763e0
add tests for http transports Brian Kassouf 7 years ago
2 changed file(s) with 78 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
0 package jwt_test
0 package jwt
11
22 import (
33 "testing"
44
5 stdjwt "github.com/dgrijalva/jwt-go"
5 jwt "github.com/dgrijalva/jwt-go"
66
7 "github.com/go-kit/kit/auth/jwt"
87 "golang.org/x/net/context"
98 )
109
1110 var (
1211 key = "test_signing_key"
13 method = stdjwt.SigningMethodHS256
14 claims = stdjwt.MapClaims{"user": "go-kit"}
12 method = jwt.SigningMethodHS256
13 claims = jwt.MapClaims{"user": "go-kit"}
1514 signedKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.MMefQU5pwDeoWBSdyagqNlr1tDGddGUOMGiIWmMlFvk"
1615 )
1716
1817 func TestSigner(t *testing.T) {
1918 e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
2019
21 signer := jwt.NewSigner(key, method, claims)(e)
20 signer := NewSigner(key, method, claims)(e)
2221 ctx := context.Background()
2322 ctx1, err := signer(ctx, struct{}{})
2423 if err != nil {
2524 t.Fatalf("Signer returned error: %s", err)
2625 }
2726
28 token, ok := ctx1.(context.Context).Value(jwt.JWTTokenContextKey).(string)
27 token, ok := ctx1.(context.Context).Value(JWTTokenContextKey).(string)
2928 if !ok {
3029 t.Fatal("Token did not exist in context")
3130 }
3837 func TestJWTParser(t *testing.T) {
3938 e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
4039
41 keyfunc := func(token *stdjwt.Token) (interface{}, error) { return []byte(key), nil }
40 keyfunc := func(token *jwt.Token) (interface{}, error) { return []byte(key), nil }
4241
43 parser := jwt.NewParser(keyfunc, method)(e)
44 ctx := context.WithValue(context.Background(), jwt.JWTTokenContextKey, signedKey)
42 parser := NewParser(keyfunc, method)(e)
43 ctx := context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
4544 ctx1, err := parser(ctx, struct{}{})
4645 if err != nil {
4746 t.Fatalf("Parser returned error: %s", err)
4847 }
4948
50 cl, ok := ctx1.(context.Context).Value(jwt.JWTClaimsContextKey).(stdjwt.MapClaims)
49 cl, ok := ctx1.(context.Context).Value(JWTClaimsContextKey).(jwt.MapClaims)
5150 if !ok {
5251 t.Fatal("Claims were not passed into context correctly")
5352 }
0 package jwt_test
0 package jwt
11
22 import (
33 "fmt"
4 "net/http"
45 "testing"
56
67 "google.golang.org/grpc/metadata"
78
8 "github.com/go-kit/kit/auth/jwt"
99 "golang.org/x/net/context"
1010 )
1111
12 func TestToHTTPContext(t *testing.T) {
13 reqFunc := ToHTTPContext()
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 TestFromHTTPContext(t *testing.T) {
42 reqFunc := FromHTTPContext()
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
1267 func TestToGRPCContext(t *testing.T) {
1368 md := metadata.MD{}
14 reqFunc := jwt.ToGRPCContext()
69 reqFunc := ToGRPCContext()
1570
1671 // No Authorization header is passed
1772 ctx := reqFunc(context.Background(), &md)
18 token := ctx.Value(jwt.JWTTokenContextKey)
73 token := ctx.Value(JWTTokenContextKey)
1974 if token != nil {
20 t.Fatal("Context should not contain a JWT Token")
75 t.Error("Context should not contain a JWT Token")
2176 }
2277
2378 // Invalid Authorization header is passed
2479 md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
2580 ctx = reqFunc(context.Background(), &md)
26 token = ctx.Value(jwt.JWTTokenContextKey)
81 token = ctx.Value(JWTTokenContextKey)
2782 if token != nil {
28 t.Fatal("Context should not contain a JWT Token")
83 t.Error("Context should not contain a JWT Token")
2984 }
3085
3186 // Authorization header is correct
3287 md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
3388 ctx = reqFunc(context.Background(), &md)
34 token, ok := ctx.Value(jwt.JWTTokenContextKey).(string)
89 token, ok := ctx.Value(JWTTokenContextKey).(string)
3590 if !ok {
3691 t.Fatal("JWT Token not passed to context correctly")
3792 }
3893
3994 if token != signedKey {
40 t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token)
95 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token)
4196 }
4297 }
4398
4499 func TestFromGRPCContext(t *testing.T) {
45 reqFunc := jwt.FromGRPCContext()
100 reqFunc := FromGRPCContext()
46101
47102 // No JWT Token is passed in the context
48103 ctx := context.Background()
51106
52107 _, ok := md["authorization"]
53108 if ok {
54 t.Fatal("authorization key should not exist in metadata")
109 t.Error("authorization key should not exist in metadata")
55110 }
56111
57112 // Correct JWT Token is passed in the context
58 ctx = context.WithValue(context.Background(), jwt.JWTTokenContextKey, signedKey)
113 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
59114 md = metadata.MD{}
60115 reqFunc(ctx, &md)
61116
65120 }
66121
67122 if token[0] != generateAuthHeaderFromToken(signedKey) {
68 t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
123 t.Errorf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
69124 }
70125 }
71
72 func generateAuthHeaderFromToken(token string) string {
73 return fmt.Sprintf("Bearer %s", token)
74 }