...

Source file src/github.com/go-kit/kit/auth/jwt/transport_test.go

Documentation: github.com/go-kit/kit/auth/jwt

     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(JWTContextKey) != 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(JWTContextKey) != 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(JWTContextKey).(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 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 is passed in the context
    55  	ctx = context.WithValue(context.Background(), JWTContextKey, 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; 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(JWTContextKey)
    74  	if token != nil {
    75  		t.Error("Context should not contain a JWT")
    76  	}
    77  
    78  	// Invalid Authorization header is passed
    79  	md["authorization"] = []string{signedKey}
    80  	ctx = reqFunc(context.Background(), md)
    81  	token = ctx.Value(JWTContextKey)
    82  	if token != nil {
    83  		t.Error("Context should not contain a JWT")
    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(JWTContextKey).(string)
    90  	if !ok {
    91  		t.Fatal("JWT not passed to context correctly")
    92  	}
    93  
    94  	if token != signedKey {
    95  		t.Errorf("JWTs 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 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 is passed in the context
   113  	ctx = context.WithValue(context.Background(), JWTContextKey, signedKey)
   114  	md = metadata.MD{}
   115  	reqFunc(ctx, &md)
   116  
   117  	token, ok := md["authorization"]
   118  	if !ok {
   119  		t.Fatal("JWT not passed to metadata correctly")
   120  	}
   121  
   122  	if token[0] != generateAuthHeaderFromToken(signedKey) {
   123  		t.Errorf("JWTs did not match: expecting %s got %s", signedKey, token[0])
   124  	}
   125  }
   126  

View as plain text