forked from MicahParks/keyfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_test.go
164 lines (142 loc) · 4 KB
/
get_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package keyfunc_test
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/BeProductable/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)
}
}
func TestJWKS_RefreshCancelCtx(t *testing.T) {
tests := map[string]struct {
provideOptionsCtx bool
cancelOptionsCtx bool
expectedRefreshes int
}{
"cancel Options.Ctx": {
provideOptionsCtx: true,
cancelOptionsCtx: true,
expectedRefreshes: 2,
},
"do not cancel Options.Ctx": {
provideOptionsCtx: true,
cancelOptionsCtx: false,
expectedRefreshes: 3,
},
"do not provide Options.Ctx": {
provideOptionsCtx: false,
cancelOptionsCtx: false,
expectedRefreshes: 3,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var (
ctx context.Context
cancel = func() {}
)
if tc.provideOptionsCtx {
ctx, cancel = context.WithCancel(context.Background())
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: 1 * time.Second,
}
jwks, err := keyfunc.Get(jwksURL, opts)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
// Wait for the first refresh to occur to ensure the
// JWKS gets refreshed at least once.
time.Sleep(1100 * time.Millisecond)
if tc.cancelOptionsCtx {
cancel()
}
// Wait for another refresh cycle to occur to ensure the
// JWKS either did or did not get refreshed depending on
// whether the passed Options.Ctx has been canceled.
time.Sleep(1101 * time.Millisecond)
jwks.EndBackground()
// Wait for another refresh cycle to occur to verify that
// the JWKS did not get refreshed after EndBackground()
// has been called.
time.Sleep(1100 * time.Millisecond)
count := atomic.LoadUint64(&counter)
if count != uint64(tc.expectedRefreshes) {
t.Fatalf("Expected %d refreshes, got %d.", tc.expectedRefreshes, count)
}
})
}
}