token_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2014 The oauth2 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. package oauth2
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. func TestTokenExtra(t *testing.T) {
  10. type testCase struct {
  11. key string
  12. val interface{}
  13. want interface{}
  14. }
  15. const key = "extra-key"
  16. cases := []testCase{
  17. {key: key, val: "abc", want: "abc"},
  18. {key: key, val: 123, want: 123},
  19. {key: key, val: "", want: ""},
  20. {key: "other-key", val: "def", want: nil},
  21. }
  22. for _, tc := range cases {
  23. extra := make(map[string]interface{})
  24. extra[tc.key] = tc.val
  25. tok := &Token{raw: extra}
  26. if got, want := tok.Extra(key), tc.want; got != want {
  27. t.Errorf("Extra(%q) = %q; want %q", key, got, want)
  28. }
  29. }
  30. }
  31. func TestTokenExpiry(t *testing.T) {
  32. now := time.Now()
  33. cases := []struct {
  34. name string
  35. tok *Token
  36. want bool
  37. }{
  38. {name: "12 seconds", tok: &Token{Expiry: now.Add(12 * time.Second)}, want: false},
  39. {name: "10 seconds", tok: &Token{Expiry: now.Add(expiryDelta)}, want: true},
  40. }
  41. for _, tc := range cases {
  42. if got, want := tc.tok.expired(), tc.want; got != want {
  43. t.Errorf("expired (%q) = %v; want %v", tc.name, got, want)
  44. }
  45. }
  46. }