Skip to content

Commit

Permalink
Manage time resetting better
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Meves committed Feb 5, 2021
1 parent 9a56370 commit 6cd4adf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 10 additions & 2 deletions mockoidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,17 @@ func (m *MockOIDC) Now() time.Time {
return NowFunc().Add(m.fastForward)
}

// Synchronize sets the jwt.TimeFunc to our mutated view of time
func (m *MockOIDC) Synchronize() {
// TimeReset is a function that resets time
type TimeReset func()

// Synchronize sets the jwt.TimeFunc to our mutated view of time.
// It returns a func that can reset it to its original state.
func (m *MockOIDC) Synchronize() TimeReset {
original := jwt.TimeFunc

jwt.TimeFunc = m.Now

return func() { jwt.TimeFunc = original }
}

func (m *MockOIDC) Addr() string {
Expand Down
7 changes: 2 additions & 5 deletions mockoidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"testing"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/oauth2-proxy/mockoidc/v1"
Expand All @@ -28,10 +27,8 @@ func TestRun(t *testing.T) {
defer m.Shutdown()

// Override jwt.TimeFunc with our timer
m.Synchronize()
defer func() {
jwt.TimeFunc = time.Now
}()
reset := m.Synchronize()
defer reset()

// ************************************************************************
// Stage 0: Get Discovery documents
Expand Down

0 comments on commit 6cd4adf

Please sign in to comment.