-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_test.go
107 lines (100 loc) · 2.38 KB
/
metadata_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
package oauth2
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func Test_buildMetadata(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want *ServerMetadata
}{
{
name: "Happy path",
args: args{
url: "http://localhost:8000",
},
want: &ServerMetadata{
Issuer: "http://localhost:8000",
AuthorizationEndpoint: "http://localhost:8000/authorize",
TokenEndpoint: "http://localhost:8000/token",
JWKSURI: "http://localhost:8000/certs",
SupportedScopes: []string{"profile"},
SupportedResponseTypes: []string{"code"},
SupportedGrantTypes: []string{"authorization_code", "client_credentials", "refresh_token"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildMetadata(tt.args.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildMetadata() = %v, want %v", got, tt.want)
}
})
}
}
func TestAuthorizationServer_handleMetadata(t *testing.T) {
type fields struct {
metadata *ServerMetadata
}
type args struct {
r *http.Request
}
tests := []struct {
name string
fields fields
args args
want *ServerMetadata
wantCode int
}{
{
name: "wrong method",
fields: fields{},
args: args{
r: httptest.NewRequest("POST", "/.well-known/openid-configuration", nil),
},
want: nil,
wantCode: http.StatusMethodNotAllowed,
},
{
name: "valid metadata",
fields: fields{
metadata: buildMetadata(DefaultAddress),
},
args: args{
r: httptest.NewRequest("GET", "/.well-known/openid-configuration", nil),
},
want: buildMetadata(DefaultAddress),
wantCode: 200,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := &AuthorizationServer{
metadata: tt.fields.metadata,
}
rr := httptest.NewRecorder()
srv.handleMetadata(rr, tt.args.r)
gotCode := rr.Code
if gotCode != tt.wantCode {
t.Errorf("AuthorizationServer.handleMetadata() code = %v, wantCode %v", gotCode, tt.wantCode)
}
if rr.Code == http.StatusOK {
var got ServerMetadata
err := json.Unmarshal(rr.Body.Bytes(), &got)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(&got, tt.want) {
t.Errorf("AuthorizationServer.handleMetadata() = %v, want %v", got, tt.want)
}
}
})
}
}