You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

73 lines
2.0 KiB

  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. 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. {name: "-1 hour", tok: &Token{Expiry: now.Add(-1 * time.Hour)}, want: true},
  41. }
  42. for _, tc := range cases {
  43. if got, want := tc.tok.expired(), tc.want; got != want {
  44. t.Errorf("expired (%q) = %v; want %v", tc.name, got, want)
  45. }
  46. }
  47. }
  48. func TestTokenTypeMethod(t *testing.T) {
  49. cases := []struct {
  50. name string
  51. tok *Token
  52. want string
  53. }{
  54. {name: "bearer-mixed_case", tok: &Token{TokenType: "beAREr"}, want: "Bearer"},
  55. {name: "default-bearer", tok: &Token{}, want: "Bearer"},
  56. {name: "basic", tok: &Token{TokenType: "basic"}, want: "Basic"},
  57. {name: "basic-capitalized", tok: &Token{TokenType: "Basic"}, want: "Basic"},
  58. {name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
  59. {name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
  60. {name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
  61. }
  62. for _, tc := range cases {
  63. if got, want := tc.tok.Type(), tc.want; got != want {
  64. t.Errorf("TokenType(%q) = %v; want %v", tc.name, got, want)
  65. }
  66. }
  67. }