-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
350 lines (313 loc) · 9.84 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package rmhttp
import (
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// ------------------------------------------------------------------------------------------------
// CONFIG TESTS
// ------------------------------------------------------------------------------------------------
var defaultTimeoutConfig = TimeoutConfig{
TCPReadTimeout: 2,
TCPReadHeaderTimeout: 1,
TCPIdleTimeout: 120,
TCPWriteTimeout: 5,
TCPWriteTimeoutPadding: 1,
RequestTimeout: 7,
TimeoutMessage: "Request Timeout",
}
var defaultSSLConfig = SSLConfig{
Enable: false,
Cert: "",
Key: "",
}
var defaultConfig = Config{
Host: "",
Port: 8080,
Debug: false,
Logger: nil,
SSL: defaultSSLConfig,
Timeout: defaultTimeoutConfig,
}
// Test_LoadConfig_default tests the default config. It simulates no user config being passed
// and no related environment variables being set.
func Test_LoadConfig_default(t *testing.T) {
cfg, err := LoadConfig(Config{})
if err != nil {
t.Errorf("LoadConfig returned error: %v", err)
}
tests := []struct {
Name string
Value any
Expected any
}{
{"default host", cfg.Host, defaultConfig.Host},
{"default port", cfg.Port, defaultConfig.Port},
{"default debug flag", cfg.Debug, defaultConfig.Debug},
{"default logger", cfg.Logger, defaultConfig.Logger},
{"default SSL config", cfg.SSL, defaultConfig.SSL},
{"default timeout config", cfg.Timeout, defaultConfig.Timeout},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, test.Value, "they should be equal")
})
}
}
// Test_LoadConfig_from_env tests the default config, but with environment variables set. It simulates where the
// related environment variables have been set.
func Test_LoadConfig_from_env(t *testing.T) {
// Config related env variables
host := "localhost"
port := 80
debug := true
enablePanicRecovery := true
enableHTTPErrorHandling := true
enableHTTPLogging := true
loggerAllowedMethods := []string{"GET", "POST"}
// SSLConfig related env variables and config
sslEnable := true
sslCert := "/path/to/cert"
sslKey := "/path/to/key"
envSSLConfig := SSLConfig{
Enable: sslEnable,
Cert: sslCert,
Key: sslKey,
}
// TimeoutConfig related env variables and config
tcpReadTimeout := 10
tcpReadHeaderTimeout := 10
tcpIdleTimeout := 10
tcpWriteTimeout := 10
tcpWriteTimeoutBuffer := 10
httpRequestTimeout := 10
timeoutMessage := "Hello, World!"
envTimeoutConfig := TimeoutConfig{
TCPReadTimeout: tcpReadTimeout,
TCPReadHeaderTimeout: tcpReadHeaderTimeout,
TCPIdleTimeout: tcpIdleTimeout,
TCPWriteTimeout: tcpWriteTimeout,
TCPWriteTimeoutPadding: tcpWriteTimeoutBuffer,
RequestTimeout: httpRequestTimeout,
TimeoutMessage: timeoutMessage,
}
vars := map[string]string{
"HOST": host,
"PORT": strconv.Itoa(port),
"DEBUG": strconv.FormatBool(debug),
"ENABLE_PANIC_RECOVERY": strconv.FormatBool(enablePanicRecovery),
"ENABLE_HTTP_ERROR_HANDLING": strconv.FormatBool(enableHTTPErrorHandling),
"ENABLE_HTTP_LOGGING": strconv.FormatBool(enableHTTPLogging),
"LOGGER_ALLOWED_METHODS": strings.Join(loggerAllowedMethods, ","),
"ENABLE_SSL": strconv.FormatBool(sslEnable),
"SSL_CERT": sslCert,
"SSL_KEY": sslKey,
"TCP_READ_TIMEOUT": strconv.Itoa(tcpReadTimeout),
"TCP_READ_HEADER_TIMEOUT": strconv.Itoa(tcpReadHeaderTimeout),
"TCP_IDLE_TIMEOUT": strconv.Itoa(tcpIdleTimeout),
"TCP_WRITE_TIMEOUT": strconv.Itoa(tcpWriteTimeout),
"TCP_WRITE_TIMEOUT_BUFFER": strconv.Itoa(tcpWriteTimeoutBuffer),
"HTTP_REQUEST_TIMEOUT": strconv.Itoa(httpRequestTimeout),
"HTTP_TIMEOUT_MESSAGE": timeoutMessage,
}
// Set the environment variables
for key, value := range vars {
os.Setenv(key, value)
}
cfg, err := LoadConfig(Config{})
if err != nil {
t.Errorf("LoadConfig returned error: %v", err)
}
tests := []struct {
Name string
Value any
Expected any
}{
{"host set from an environment variable", cfg.Host, host},
{"port set from an environment variable", cfg.Port, port},
{"debug flag set from an environment variable", cfg.Debug, debug},
{"SSL config set from environment variables", cfg.SSL, envSSLConfig},
{"timeout config set from environment variables", cfg.Timeout, envTimeoutConfig},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, test.Value, "they should be equal")
})
}
// Clean up the environment variables
for key := range vars {
os.Unsetenv(key)
}
}
// Test_LoadConfig_with_user_defined_config tests how the default config handles being merged with a user config.
// It simulates a complete user config being passed.
func Test_LoadConfig_with_user_defined_config(t *testing.T) {
// Config related env variables
host := "localhost"
port := 80
debug := true
// SSLConfig related env variables and config
sslEnable := true
sslCert := "/path/to/cert"
sslKey := "/path/to/key"
userSSLConfig := SSLConfig{
Enable: sslEnable,
Cert: sslCert,
Key: sslKey,
}
// TimeoutConfig related env variables and config
tcpReadTimeout := 10
tcpReadHeaderTimeout := 10
tcpIdleTimeout := 10
tcpWriteTimeout := 10
tcpWriteTimeoutBuffer := 10
httpRequestTimeout := 10
timeoutMessage := "Hello, World!"
userTimeoutConfig := TimeoutConfig{
TCPReadTimeout: tcpReadTimeout,
TCPReadHeaderTimeout: tcpReadHeaderTimeout,
TCPIdleTimeout: tcpIdleTimeout,
TCPWriteTimeout: tcpWriteTimeout,
TCPWriteTimeoutPadding: tcpWriteTimeoutBuffer,
RequestTimeout: httpRequestTimeout,
TimeoutMessage: timeoutMessage,
}
userConfig := Config{
Host: host,
Port: port,
Debug: debug,
SSL: userSSLConfig,
Timeout: userTimeoutConfig,
}
cfg, err := LoadConfig(userConfig)
if err != nil {
t.Errorf("LoadConfig returned error: %v", err)
}
tests := []struct {
Name string
Value any
Expected any
}{
{"host set from a user defined config", cfg.Host, host},
{"port set from a user defined config", cfg.Port, port},
{"debug flag set from a user defined config", cfg.Debug, debug},
{"SSL config set from a user defined config", cfg.SSL, userSSLConfig},
{"timeout config set from a user defined config", cfg.Timeout, userTimeoutConfig},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, test.Value, "they should be equal")
})
}
}
// Test_LoadConfig_with_user_partially_defined_config tests how the default config handles being merged with a user config.
// It simulates a partially completed user config being passed.
func Test_LoadConfig_with_user_partially_defined_config(t *testing.T) {
// Config related env variables
host := "localhost"
// port := 80
// debug := true
// SSLConfig related env variables and config
// sslEnable := true
// sslCert := "/path/to/cert"
sslKey := "/path/to/key"
partialSSLConfig := SSLConfig{
// Enable: sslEnable,
// Cert: sslCert,
Key: sslKey,
}
// TimeoutConfig related env variables and config
tcpReadTimeout := 10
// tcpReadHeaderTimeout := 10
// tcpIdleTimeout := 10
// tcpWriteTimeout := 10
tcpWriteTimeoutBuffer := 10
httpRequestTimeout := 10
timeoutMessage := "Hello, World!"
partialTimeoutConfig := TimeoutConfig{
TCPReadTimeout: tcpReadTimeout,
// TCPReadHeaderTimeout: tcpReadHeaderTimeout,
// TCPIdleTimeout: tcpIdleTimeout,
// TCPWriteTimeout: tcpWriteTimeout,
TCPWriteTimeoutPadding: tcpWriteTimeoutBuffer,
RequestTimeout: httpRequestTimeout,
TimeoutMessage: timeoutMessage,
}
partialConfig := Config{
Host: host,
// Port: port,
// Debug: debug,
SSL: partialSSLConfig,
Timeout: partialTimeoutConfig,
}
cfg, err := LoadConfig(partialConfig)
if err != nil {
t.Errorf("LoadConfig returned error: %v", err)
}
tests := []struct {
Name string
Value any
Expected any
}{
{"host set from a partially defined user config", cfg.Host, host},
{"port set from a partially defined user config", cfg.Port, defaultConfig.Port},
{"debug flag set from a partially defined user config", cfg.Debug, defaultConfig.Debug},
{
"enable SSL flag set from a partially defined user config",
cfg.SSL.Enable,
defaultSSLConfig.Enable,
},
{
"SSL certificate path set from a partially defined user config",
cfg.SSL.Cert,
defaultSSLConfig.Cert,
},
{
"SSL key path set from a partially defined user config",
cfg.SSL.Key,
partialSSLConfig.Key,
},
{
"TCP read timeout set from a partially defined user config",
cfg.Timeout.TCPReadTimeout,
partialTimeoutConfig.TCPReadTimeout,
},
{
"TCP read header timeout set from a partially defined user config",
cfg.Timeout.TCPReadHeaderTimeout,
defaultTimeoutConfig.TCPReadHeaderTimeout,
},
{
"TCP idle timeout set from a partially defined user config",
cfg.Timeout.TCPIdleTimeout,
defaultTimeoutConfig.TCPIdleTimeout,
},
{
"TCP write timeout set from a partially defined user config",
cfg.Timeout.TCPWriteTimeout,
defaultTimeoutConfig.TCPWriteTimeout,
},
{
"TCP write timeout buffer set from a partially defined user config",
cfg.Timeout.TCPWriteTimeoutPadding,
partialTimeoutConfig.TCPWriteTimeoutPadding,
},
{
"HTTP request timeout set from a partially defined user config",
cfg.Timeout.RequestTimeout,
partialTimeoutConfig.RequestTimeout,
},
{
"timeout message set from a partially defined user config",
cfg.Timeout.TimeoutMessage,
partialTimeoutConfig.TimeoutMessage,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, test.Value, "they should be equal")
})
}
}