-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from MicahParks/manual_refresh
Add support for manual JWKS refresh
- Loading branch information
Showing
5 changed files
with
163 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package keyfunc_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/MicahParks/keyfunc" | ||
) | ||
|
||
func TestJWKS_Refresh(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
var counter uint64 | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
atomic.AddUint64(&counter, 1) | ||
_, err := w.Write([]byte(jwksJSON)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
jwksURL := server.URL | ||
opts := keyfunc.Options{ | ||
Ctx: ctx, | ||
} | ||
jwks, err := keyfunc.Get(jwksURL, opts) | ||
if err != nil { | ||
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err) | ||
} | ||
|
||
err = jwks.Refresh(ctx, keyfunc.RefreshOptions{IgnoreRateLimit: true}) | ||
if err != nil { | ||
t.Fatalf(logFmt, "Failed to refresh JWKS.", err) | ||
} | ||
|
||
count := atomic.LoadUint64(&counter) | ||
if count != 2 { | ||
t.Fatalf("Expected 2 refreshes, got %d.", count) | ||
} | ||
} | ||
|
||
func TestJWKS_RefreshUsingBackgroundGoroutine(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
var counter uint64 | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
atomic.AddUint64(&counter, 1) | ||
_, err := w.Write([]byte(jwksJSON)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
jwksURL := server.URL | ||
opts := keyfunc.Options{ | ||
Ctx: ctx, | ||
RefreshInterval: time.Hour, | ||
RefreshRateLimit: time.Hour, | ||
} | ||
jwks, err := keyfunc.Get(jwksURL, opts) | ||
if err != nil { | ||
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err) | ||
} | ||
|
||
err = jwks.Refresh(ctx, keyfunc.RefreshOptions{IgnoreRateLimit: true}) | ||
if err != nil { | ||
t.Fatalf(logFmt, "Failed to refresh JWKS.", err) | ||
} | ||
|
||
count := atomic.LoadUint64(&counter) | ||
if count != 2 { | ||
t.Fatalf("Expected 2 refreshes, got %d.", count) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters