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 TestHTTPToContext(t *testing.T) {
reqFunc := HTTPToContext()
// When the header doesn't exist
ctx := reqFunc(context.Background(), &http.Request{})
if ctx.Value(JWTContextKey) != 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(JWTContextKey) != 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(JWTContextKey).(string)
if token != signedKey {
t.Errorf("Context doesn't contain the expected encoded token value; expected: %s, got: %s", signedKey, token)
}
}
func TestContextToHTTP(t *testing.T) {
reqFunc := ContextToHTTP()
// No JWT 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 is passed in the context
ctx = context.WithValue(context.Background(), JWTContextKey, 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; expected %s, got %s", expected, token)
}
}
func TestGRPCToContext(t *testing.T) {
md := metadata.MD{}
reqFunc := GRPCToContext()
// No Authorization header is passed
ctx := reqFunc(context.Background(), md)
token := ctx.Value(JWTContextKey)
if token != nil {
t.Error("Context should not contain a JWT")
}
// Invalid Authorization header is passed
md["authorization"] = []string{signedKey}
ctx = reqFunc(context.Background(), md)
token = ctx.Value(JWTContextKey)
if token != nil {
t.Error("Context should not contain a JWT")
}
// Authorization header is correct
md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
ctx = reqFunc(context.Background(), md)
token, ok := ctx.Value(JWTContextKey).(string)
if !ok {
t.Fatal("JWT not passed to context correctly")
}
if token != signedKey {
t.Errorf("JWTs did not match: expecting %s got %s", signedKey, token)
}
}
func TestContextToGRPC(t *testing.T) {
reqFunc := ContextToGRPC()
// No JWT 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 is passed in the context
ctx = context.WithValue(context.Background(), JWTContextKey, signedKey)
md = metadata.MD{}
reqFunc(ctx, &md)
token, ok := md["authorization"]
if !ok {
t.Fatal("JWT not passed to metadata correctly")
}
if token[0] != generateAuthHeaderFromToken(signedKey) {
t.Errorf("JWTs did not match: expecting %s got %s", signedKey, token[0])
}
}
|