...

Source file src/golang.org/x/oauth2/token_test.go

Documentation: golang.org/x/oauth2

     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  
     5  package oauth2
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func 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  
    35  func 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(defaultExpiryDelta)}, want: false},
    47  		{name: "10 seconds-1ns", tok: &Token{Expiry: now.Add(defaultExpiryDelta - 1*time.Nanosecond)}, want: true},
    48  		{name: "-1 hour", tok: &Token{Expiry: now.Add(-1 * time.Hour)}, want: true},
    49  		{name: "12 seconds, custom expiryDelta", tok: &Token{Expiry: now.Add(12 * time.Second), expiryDelta: time.Second * 5}, want: false},
    50  		{name: "5 seconds, custom expiryDelta", tok: &Token{Expiry: now.Add(time.Second * 5), expiryDelta: time.Second * 5}, want: false},
    51  		{name: "5 seconds-1ns, custom expiryDelta", tok: &Token{Expiry: now.Add(time.Second*5 - 1*time.Nanosecond), expiryDelta: time.Second * 5}, want: true},
    52  		{name: "-1 hour, custom expiryDelta", tok: &Token{Expiry: now.Add(-1 * time.Hour), expiryDelta: time.Second * 5}, want: true},
    53  	}
    54  	for _, tc := range cases {
    55  		if got, want := tc.tok.expired(), tc.want; got != want {
    56  			t.Errorf("expired (%q) = %v; want %v", tc.name, got, want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestTokenTypeMethod(t *testing.T) {
    62  	cases := []struct {
    63  		name string
    64  		tok  *Token
    65  		want string
    66  	}{
    67  		{name: "bearer-mixed_case", tok: &Token{TokenType: "beAREr"}, want: "Bearer"},
    68  		{name: "default-bearer", tok: &Token{}, want: "Bearer"},
    69  		{name: "basic", tok: &Token{TokenType: "basic"}, want: "Basic"},
    70  		{name: "basic-capitalized", tok: &Token{TokenType: "Basic"}, want: "Basic"},
    71  		{name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
    72  		{name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
    73  		{name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
    74  	}
    75  	for _, tc := range cases {
    76  		if got, want := tc.tok.Type(), tc.want; got != want {
    77  			t.Errorf("TokenType(%q) = %v; want %v", tc.name, got, want)
    78  		}
    79  	}
    80  }
    81  

View as plain text