選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

77 行
2.2 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. timeNow = func() time.Time { return now }
  34. defer func() { timeNow = time.Now }()
  35. cases := []struct {
  36. name string
  37. tok *Token
  38. want bool
  39. }{
  40. {name: "12 seconds", tok: &Token{Expiry: now.Add(12 * time.Second)}, want: false},
  41. {name: "10 seconds", tok: &Token{Expiry: now.Add(expiryDelta)}, want: false},
  42. {name: "10 seconds-1ns", tok: &Token{Expiry: now.Add(expiryDelta - 1*time.Nanosecond)}, want: true},
  43. {name: "-1 hour", tok: &Token{Expiry: now.Add(-1 * time.Hour)}, want: true},
  44. }
  45. for _, tc := range cases {
  46. if got, want := tc.tok.expired(), tc.want; got != want {
  47. t.Errorf("expired (%q) = %v; want %v", tc.name, got, want)
  48. }
  49. }
  50. }
  51. func TestTokenTypeMethod(t *testing.T) {
  52. cases := []struct {
  53. name string
  54. tok *Token
  55. want string
  56. }{
  57. {name: "bearer-mixed_case", tok: &Token{TokenType: "beAREr"}, want: "Bearer"},
  58. {name: "default-bearer", tok: &Token{}, want: "Bearer"},
  59. {name: "basic", tok: &Token{TokenType: "basic"}, want: "Basic"},
  60. {name: "basic-capitalized", tok: &Token{TokenType: "Basic"}, want: "Basic"},
  61. {name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
  62. {name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
  63. {name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
  64. }
  65. for _, tc := range cases {
  66. if got, want := tc.tok.Type(), tc.want; got != want {
  67. t.Errorf("TokenType(%q) = %v; want %v", tc.name, got, want)
  68. }
  69. }
  70. }