102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func clearXYZEnv(t *testing.T) {
|
|
t.Helper()
|
|
keys := []string{
|
|
"XYZ_LISTEN", "XYZ_DATA_DIR", "XYZ_ONLINE_MINUTES",
|
|
"XYZ_TRUST_PROXY", "XYZ_COUNT_HTTPS_ONLY", "XYZ_TLS_CERT", "XYZ_TLS_KEY", "XYZ_DEBUG_LOG",
|
|
}
|
|
for _, key := range keys {
|
|
clearOneEnv(t, key)
|
|
}
|
|
}
|
|
|
|
func clearOneEnv(t *testing.T, key string) {
|
|
t.Helper()
|
|
val, ok := os.LookupEnv(key)
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if ok {
|
|
_ = os.Setenv(key, val)
|
|
} else {
|
|
_ = os.Unsetenv(key)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestConfigDefaults(t *testing.T) {
|
|
clearXYZEnv(t)
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Listen != "127.0.0.1:8080" || cfg.DataDir != "./data" || cfg.OnlineMinutes != 15 || cfg.TrustProxy || cfg.HTTPSOnly || cfg.DebugLog || cfg.TLSCert != "" || cfg.TLSKey != "" {
|
|
t.Fatalf("%+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestConfigValuesAndRejects(t *testing.T) {
|
|
clearXYZEnv(t)
|
|
t.Setenv("XYZ_LISTEN", "127.0.0.1:9090")
|
|
t.Setenv("XYZ_DATA_DIR", `D:\xyz-data`)
|
|
t.Setenv("XYZ_ONLINE_MINUTES", "120")
|
|
t.Setenv("XYZ_TRUST_PROXY", "true")
|
|
t.Setenv("XYZ_COUNT_HTTPS_ONLY", "true")
|
|
t.Setenv("XYZ_DEBUG_LOG", "1")
|
|
t.Setenv("XYZ_TLS_CERT", "cert.pem")
|
|
t.Setenv("XYZ_TLS_KEY", "key.pem")
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Listen != "127.0.0.1:9090" || cfg.DataDir != `D:\xyz-data` || cfg.OnlineMinutes != 120 || !cfg.TrustProxy || !cfg.HTTPSOnly || !cfg.DebugLog || cfg.TLSCert != "cert.pem" || cfg.TLSKey != "key.pem" {
|
|
t.Fatalf("%+v", cfg)
|
|
}
|
|
|
|
cases := []struct {
|
|
key, val, needle string
|
|
}{
|
|
{"XYZ_LISTEN", "bad", "XYZ_LISTEN"},
|
|
{"XYZ_DATA_DIR", "", "XYZ_DATA_DIR"},
|
|
{"XYZ_ONLINE_MINUTES", "0", "XYZ_ONLINE_MINUTES"},
|
|
{"XYZ_ONLINE_MINUTES", "121", "XYZ_ONLINE_MINUTES"},
|
|
{"XYZ_ONLINE_MINUTES", "abc", "XYZ_ONLINE_MINUTES"},
|
|
{"XYZ_TRUST_PROXY", "yes", "XYZ_TRUST_PROXY"},
|
|
{"XYZ_COUNT_HTTPS_ONLY", "1", "XYZ_COUNT_HTTPS_ONLY"},
|
|
{"XYZ_DEBUG_LOG", "2", "XYZ_DEBUG_LOG"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.key+"="+tc.val, func(t *testing.T) {
|
|
clearXYZEnv(t)
|
|
t.Setenv("XYZ_LISTEN", "127.0.0.1:8080")
|
|
t.Setenv("XYZ_DATA_DIR", "data")
|
|
t.Setenv("XYZ_ONLINE_MINUTES", "15")
|
|
t.Setenv("XYZ_TRUST_PROXY", "false")
|
|
t.Setenv("XYZ_COUNT_HTTPS_ONLY", "false")
|
|
t.Setenv("XYZ_DEBUG_LOG", "0")
|
|
t.Setenv(tc.key, tc.val)
|
|
_, err := loadConfig()
|
|
if err == nil || !strings.Contains(err.Error(), tc.needle) {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("tls half", func(t *testing.T) {
|
|
clearXYZEnv(t)
|
|
t.Setenv("XYZ_TLS_CERT", "cert.pem")
|
|
_, err := loadConfig()
|
|
if err == nil || !strings.Contains(err.Error(), "XYZ_TLS") {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
}
|