2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-12-16 21:43:35 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package oauth2
|
|
|
|
|
2015-01-26 21:43:56 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2014-12-16 21:43:35 +03:00
|
|
|
|
|
|
|
func TestTokenExtra(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
key string
|
|
|
|
val interface{}
|
|
|
|
want interface{}
|
|
|
|
}
|
|
|
|
const key = "extra-key"
|
|
|
|
cases := []testCase{
|
|
|
|
{key: key, val: "abc", want: "abc"},
|
|
|
|
{key: key, val: 123, want: 123},
|
|
|
|
{key: key, val: "", want: ""},
|
|
|
|
{key: "other-key", val: "def", want: nil},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
extra := make(map[string]interface{})
|
|
|
|
extra[tc.key] = tc.val
|
|
|
|
tok := &Token{raw: extra}
|
|
|
|
if got, want := tok.Extra(key), tc.want; got != want {
|
|
|
|
t.Errorf("Extra(%q) = %q; want %q", key, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 21:43:56 +03:00
|
|
|
|
|
|
|
func TestTokenExpiry(t *testing.T) {
|
|
|
|
now := time.Now()
|
2019-01-11 21:04:42 +03:00
|
|
|
timeNow = func() time.Time { return now }
|
|
|
|
defer func() { timeNow = time.Now }()
|
|
|
|
|
2015-01-26 21:43:56 +03:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
tok *Token
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{name: "12 seconds", tok: &Token{Expiry: now.Add(12 * time.Second)}, want: false},
|
2023-03-28 00:21:45 +03:00
|
|
|
{name: "10 seconds", tok: &Token{Expiry: now.Add(defaultExpiryDelta)}, want: false},
|
|
|
|
{name: "10 seconds-1ns", tok: &Token{Expiry: now.Add(defaultExpiryDelta - 1*time.Nanosecond)}, want: true},
|
2015-09-30 03:24:17 +03:00
|
|
|
{name: "-1 hour", tok: &Token{Expiry: now.Add(-1 * time.Hour)}, want: true},
|
2023-03-28 00:21:45 +03:00
|
|
|
{name: "12 seconds, custom expiryDelta", tok: &Token{Expiry: now.Add(12 * time.Second), expiryDelta: time.Second * 5}, want: false},
|
|
|
|
{name: "5 seconds, custom expiryDelta", tok: &Token{Expiry: now.Add(time.Second * 5), expiryDelta: time.Second * 5}, want: false},
|
|
|
|
{name: "5 seconds-1ns, custom expiryDelta", tok: &Token{Expiry: now.Add(time.Second*5 - 1*time.Nanosecond), expiryDelta: time.Second * 5}, want: true},
|
|
|
|
{name: "-1 hour, custom expiryDelta", tok: &Token{Expiry: now.Add(-1 * time.Hour), expiryDelta: time.Second * 5}, want: true},
|
2015-01-26 21:43:56 +03:00
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
if got, want := tc.tok.expired(), tc.want; got != want {
|
|
|
|
t.Errorf("expired (%q) = %v; want %v", tc.name, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 03:24:17 +03:00
|
|
|
|
|
|
|
func TestTokenTypeMethod(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
tok *Token
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{name: "bearer-mixed_case", tok: &Token{TokenType: "beAREr"}, want: "Bearer"},
|
|
|
|
{name: "default-bearer", tok: &Token{}, want: "Bearer"},
|
|
|
|
{name: "basic", tok: &Token{TokenType: "basic"}, want: "Basic"},
|
|
|
|
{name: "basic-capitalized", tok: &Token{TokenType: "Basic"}, want: "Basic"},
|
|
|
|
{name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
|
|
|
|
{name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
|
|
|
|
{name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
if got, want := tc.tok.Type(), tc.want; got != want {
|
|
|
|
t.Errorf("TokenType(%q) = %v; want %v", tc.name, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|