-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwks_test.go
95 lines (88 loc) · 1.82 KB
/
jwks_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
package oauth2
import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/json"
"math/big"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestAuthorizationServer_handleJWKS(t *testing.T) {
type fields struct {
clients []*Client
signingKeys map[int]*ecdsa.PrivateKey
}
type args struct {
r *http.Request
}
tests := []struct {
name string
fields fields
args args
want *JSONWebKeySet
wantCode int
}{
{
name: "retrieve JWKS with GET",
fields: fields{
signingKeys: map[int]*ecdsa.PrivateKey{
0: {
PublicKey: ecdsa.PublicKey{
Curve: elliptic.P256(),
X: big.NewInt(1),
Y: big.NewInt(2),
},
},
},
},
args: args{
r: httptest.NewRequest("GET", "/certs", nil),
},
want: &JSONWebKeySet{
Keys: []JSONWebKey{{
Kid: "0",
Kty: "EC",
Crv: "P-256",
X: "AQ",
Y: "Ag",
}},
},
wantCode: http.StatusOK,
},
{
name: "retrieve JWKS with POST",
fields: fields{},
args: args{
r: httptest.NewRequest("POST", "/certs", nil),
},
want: nil,
wantCode: http.StatusMethodNotAllowed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := &AuthorizationServer{
clients: tt.fields.clients,
signingKeys: tt.fields.signingKeys,
}
rr := httptest.NewRecorder()
srv.handleJWKS(rr, tt.args.r)
gotCode := rr.Code
if gotCode != tt.wantCode {
t.Errorf("AuthorizationServer.handleJWKS() code = %v, wantCode %v", gotCode, tt.wantCode)
}
if rr.Code == http.StatusOK {
var got JSONWebKeySet
err := json.Unmarshal(rr.Body.Bytes(), &got)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(&got, tt.want) {
t.Errorf("AuthorizationServer.handleJWKS() = %v, want %v", got, tt.want)
}
}
})
}
}