-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_test.go
159 lines (144 loc) · 4.28 KB
/
http_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
package jwkset
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
func TestClient(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
kid := "my-key-id"
secret := []byte("my-hmac-secret")
serverStore := NewMemoryStorage()
marshalOptions := JWKMarshalOptions{
Private: true,
}
metadata := JWKMetadataOptions{
KID: kid,
}
options := JWKOptions{
Marshal: marshalOptions,
Metadata: metadata,
}
jwk, err := NewJWKFromKey(secret, options)
if err != nil {
t.Fatalf("Failed to create a JWK from the given HMAC secret.\nError: %s", err)
}
err = serverStore.KeyWrite(ctx, jwk)
if err != nil {
t.Fatalf("Failed to write the given JWK to the store.\nError: %s", err)
}
rawJWKS, err := serverStore.JSON(ctx)
if err != nil {
t.Fatalf("Failed to get the JSON.\nError: %s", err)
}
rawJWKSMux := sync.RWMutex{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawJWKSMux.RLock()
defer rawJWKSMux.RUnlock()
_, _ = w.Write(rawJWKS)
}))
clientStore, err := NewDefaultHTTPClient([]string{server.URL})
if err != nil {
t.Fatalf("Failed to create a new HTTP client.\nError: %s", err)
}
jwk, err = clientStore.KeyRead(ctx, kid)
if err != nil {
t.Fatalf("Failed to read the JWK.\nError: %s", err)
}
if !bytes.Equal(jwk.Key().([]byte), secret) {
t.Fatalf("The key read from the HTTP client did not match the original key.")
}
jwks, err := clientStore.KeyReadAll(ctx)
if err != nil {
t.Fatalf("Failed to read all the JWKs.\nError: %s", err)
}
if len(jwks) != 1 {
t.Fatalf("Expected to read 1 JWK, but got %d.", len(jwks))
}
if !bytes.Equal(jwks[0].Key().([]byte), secret) {
t.Fatalf("The key read from the HTTP client did not match the original key.")
}
ok, err := clientStore.KeyDelete(ctx, kid)
if err != nil {
t.Fatalf("Failed to delete the JWK.\nError: %s", err)
}
if !ok {
t.Fatalf("Expected the key to be deleted.")
}
err = clientStore.KeyWrite(ctx, jwk)
if err != nil {
t.Fatalf("Failed to write the JWK.\nError: %s", err)
}
jwk, err = clientStore.KeyRead(ctx, kid)
if err != nil {
t.Fatalf("Failed to read the JWK.\nError: %s", err)
}
if !bytes.Equal(jwk.Key().([]byte), secret) {
t.Fatalf("The key read from the HTTP client did not match the original key.")
}
otherKeyID := myKeyID + "2"
options.Metadata.KID = otherKeyID
otherSecret := []byte("my-other-hmac-secret")
jwk, err = NewJWKFromKey(otherSecret, options)
if err != nil {
t.Fatalf("Failed to create a JWK from the given HMAC secret.\nError: %s", err)
}
err = serverStore.KeyWrite(ctx, jwk)
if err != nil {
t.Fatalf("Failed to write the given JWK to the store.\nError: %s", err)
}
rawJWKSMux.Lock()
rawJWKS, err = serverStore.JSON(ctx)
rawJWKSMux.Unlock()
if err != nil {
t.Fatalf("Failed to get the JSON.\nError: %s", err)
}
jwk, err = clientStore.KeyRead(ctx, otherKeyID)
if err != nil {
t.Fatalf("Failed to read the JWK.\nError: %s", err)
}
if !bytes.Equal(jwk.Key().([]byte), otherSecret) {
t.Fatalf("The key read from the HTTP client did not match the original key.")
}
otherOtherKey := myKeyID + "3"
options.Metadata.KID = otherOtherKey
otherOtherSecret := []byte("my-other-other-hmac-secret")
jwk, err = NewJWKFromKey(otherOtherSecret, options)
if err != nil {
t.Fatalf("Failed to create a JWK from the given HMAC secret.\nError: %s", err)
}
err = serverStore.KeyWrite(ctx, jwk)
if err != nil {
t.Fatalf("Failed to write the given JWK to the store.\nError: %s", err)
}
rawJWKSMux.Lock()
rawJWKS, err = serverStore.JSON(ctx)
rawJWKSMux.Unlock()
if err != nil {
t.Fatalf("Failed to get the JSON.\nError: %s", err)
}
shortCtx, shortCancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer shortCancel()
jwk, err = clientStore.KeyRead(shortCtx, otherOtherKey)
if err == nil || !strings.HasSuffix(err.Error(), "rate: Wait(n=1) would exceed context deadline") {
t.Fatalf("Expected to exceed context deadline, but got %s.", err)
}
}
func TestClientError(t *testing.T) {
_, err := NewHTTPClient(HTTPClientOptions{})
if err == nil {
t.Fatalf("Expected an error when creating a new HTTP client without any URLs.")
}
}
func TestClientJSON(t *testing.T) {
c := httpClient{
given: NewMemoryStorage(),
}
testJSON(context.Background(), t, c)
}