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.

discovery_cache_test.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package openid
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. type testDiscoveredInfo struct{}
  10. func (s *testDiscoveredInfo) ClaimedID() string {
  11. return "claimedID"
  12. }
  13. func (s *testDiscoveredInfo) OpEndpoint() string {
  14. return "opEndpoint"
  15. }
  16. func (s *testDiscoveredInfo) OpLocalID() string {
  17. return "opLocalID"
  18. }
  19. func TestTimedDiscoveryCache(t *testing.T) {
  20. dc := newTimedDiscoveryCache(1 * time.Second)
  21. // Put some initial values
  22. dc.Put("foo", &testDiscoveredInfo{}) //openid.opEndpoint: "a", openid.opLocalID: "b", openid.claimedID: "c"})
  23. // Make sure we can retrieve them
  24. if di := dc.Get("foo"); di == nil {
  25. t.Errorf("Expected a result, got nil")
  26. } else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
  27. t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
  28. }
  29. // Attempt to get a non-existent value
  30. if di := dc.Get("bar"); di != nil {
  31. t.Errorf("Expected nil, got %v", di)
  32. }
  33. // Sleep one second and try retrieve again
  34. time.Sleep(1 * time.Second)
  35. if di := dc.Get("foo"); di != nil {
  36. t.Errorf("Expected a nil, got a result")
  37. }
  38. }