1// Copyright 2014 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package oauth2 6 7import ( 8 "testing" 9 "time" 10) 11 12func TestTokenExtra(t *testing.T) { 13 type testCase struct { 14 key string 15 val interface{} 16 want interface{} 17 } 18 const key = "extra-key" 19 cases := []testCase{ 20 {key: key, val: "abc", want: "abc"}, 21 {key: key, val: 123, want: 123}, 22 {key: key, val: "", want: ""}, 23 {key: "other-key", val: "def", want: nil}, 24 } 25 for _, tc := range cases { 26 extra := make(map[string]interface{}) 27 extra[tc.key] = tc.val 28 tok := &Token{raw: extra} 29 if got, want := tok.Extra(key), tc.want; got != want { 30 t.Errorf("Extra(%q) = %q; want %q", key, got, want) 31 } 32 } 33} 34 35func TestTokenExpiry(t *testing.T) { 36 now := time.Now() 37 timeNow = func() time.Time { return now } 38 defer func() { timeNow = time.Now }() 39 40 cases := []struct { 41 name string 42 tok *Token 43 want bool 44 }{ 45 {name: "12 seconds", tok: &Token{Expiry: now.Add(12 * time.Second)}, want: false}, 46 {name: "10 seconds", tok: &Token{Expiry: now.Add(expiryDelta)}, want: false}, 47 {name: "10 seconds-1ns", tok: &Token{Expiry: now.Add(expiryDelta - 1*time.Nanosecond)}, want: true}, 48 {name: "-1 hour", tok: &Token{Expiry: now.Add(-1 * time.Hour)}, want: true}, 49 } 50 for _, tc := range cases { 51 if got, want := tc.tok.expired(), tc.want; got != want { 52 t.Errorf("expired (%q) = %v; want %v", tc.name, got, want) 53 } 54 } 55} 56 57func TestTokenTypeMethod(t *testing.T) { 58 cases := []struct { 59 name string 60 tok *Token 61 want string 62 }{ 63 {name: "bearer-mixed_case", tok: &Token{TokenType: "beAREr"}, want: "Bearer"}, 64 {name: "default-bearer", tok: &Token{}, want: "Bearer"}, 65 {name: "basic", tok: &Token{TokenType: "basic"}, want: "Basic"}, 66 {name: "basic-capitalized", tok: &Token{TokenType: "Basic"}, want: "Basic"}, 67 {name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"}, 68 {name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"}, 69 {name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"}, 70 } 71 for _, tc := range cases { 72 if got, want := tc.tok.Type(), tc.want; got != want { 73 t.Errorf("TokenType(%q) = %v; want %v", tc.name, got, want) 74 } 75 } 76} 77