This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
124 lines (118 loc) · 3.06 KB
/
config_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
package jcp_test
import (
"errors"
"testing"
"github.com/MicahParks/jsontype"
"github.com/MicahParks/jcp"
)
const (
validURL = "https://localhost"
)
func TestConfig_DefaultsAndValidate(t *testing.T) {
validLogFormatExpected := createDefaultConfig()
validLogFormatExpected.LogFormat = jcp.LogFormatHuman
testCases := []struct {
config jcp.Config
err error
expected jcp.Config
name string
}{
{
config: jcp.Config{},
err: jcp.ErrInvalidConfig,
name: "Empty",
},
{
config: jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
validURL: {},
},
},
expected: createDefaultConfig(),
name: "EmptyWithJWKS",
},
{
config: jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
":": {},
},
},
err: jcp.ErrInvalidConfig,
name: "InvalidURL",
},
{
config: jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
"tcp://localhost": {},
},
},
err: jcp.ErrInvalidConfig,
name: "InvalidURLScheme",
},
{
config: jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
validURL: {},
},
LogFormat: jcp.LogFormatHuman,
},
expected: validLogFormatExpected,
name: "ValidLogFormat.",
},
{
config: jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
validURL: {},
},
LogFormat: "invalid",
},
err: jcp.ErrInvalidConfig,
name: "InvalidLogFormat",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
actual, err := tc.config.DefaultsAndValidate()
if err != nil || tc.err != nil {
if !errors.Is(err, tc.err) {
t.Errorf("Expected error %v, got %v.", tc.err, err)
}
return
}
if actual.ListenAddress != tc.expected.ListenAddress {
t.Errorf("Expected listen address %s, got %s.", tc.expected.ListenAddress, actual.ListenAddress)
}
if actual.LogFormat != tc.expected.LogFormat {
t.Errorf("Expected log format %s, got %s.", tc.expected.LogFormat, actual.LogFormat)
}
if actual.RequestMaxBytes != tc.expected.RequestMaxBytes {
t.Errorf("Expected request max bytes %d, got %d.", tc.expected.RequestMaxBytes, actual.RequestMaxBytes)
}
if len(actual.JWKS) != len(tc.expected.JWKS) {
t.Errorf("Expected %d JWKS, got %d.", len(tc.expected.JWKS), len(actual.JWKS))
}
for k, v := range actual.JWKS {
if v.RefreshInterval.Get() != tc.expected.JWKS[k].RefreshInterval.Get() {
t.Errorf("Expected refresh interval %v, got %v.", tc.expected.JWKS[k].RefreshInterval.Get(), v.RefreshInterval.Get())
}
if v.RefreshTimeout.Get() != tc.expected.JWKS[k].RefreshTimeout.Get() {
t.Errorf("Expected refresh timeout %v, got %v.", tc.expected.JWKS[k].RefreshTimeout.Get(), v.RefreshTimeout.Get())
}
}
})
}
}
func createDefaultConfig() jcp.Config {
return jcp.Config{
JWKS: map[string]jcp.JWKSConfig{
validURL: {
RefreshInterval: jsontype.New(jcp.DefaultRefreshInterval),
RefreshTimeout: jsontype.New(jcp.DefaultRefreshTimeout),
},
},
ListenAddress: jcp.DefaultListenAddress,
LogFormat: jcp.DefaultLogFormat,
RequestMaxBytes: jcp.DefaultRequestMaxBytes,
}
}