config_test.go
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestNewDefaults(t *testing.T) { |
| 9 | cfg := New() |
| 10 | |
| 11 | if cfg.Port != 8080 { |
| 12 | t.Errorf("expected default port 8080, got %d", cfg.Port) |
| 13 | } |
| 14 | if cfg.DataDir != "./data" { |
| 15 | t.Errorf("expected default data dir ./data, got %s", cfg.DataDir) |
| 16 | } |
| 17 | if cfg.Workers != 2 { |
| 18 | t.Errorf("expected default workers 2, got %d", cfg.Workers) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestEnvOverrides(t *testing.T) { |
| 23 | os.Setenv("VIDARCHIVE_PORT", "9090") |
| 24 | os.Setenv("VIDARCHIVE_WORKERS", "4") |
| 25 | os.Setenv("VIDARCHIVE_BASE_URL", "https://vidarchive.example.com") |
| 26 | defer func() { |
| 27 | os.Unsetenv("VIDARCHIVE_PORT") |
| 28 | os.Unsetenv("VIDARCHIVE_WORKERS") |
| 29 | os.Unsetenv("VIDARCHIVE_BASE_URL") |
| 30 | }() |
| 31 | |
| 32 | cfg := New() |
| 33 | |
| 34 | if cfg.Port != 9090 { |
| 35 | t.Errorf("expected port 9090, got %d", cfg.Port) |
| 36 | } |
| 37 | if cfg.Workers != 4 { |
| 38 | t.Errorf("expected workers 4, got %d", cfg.Workers) |
| 39 | } |
| 40 | if cfg.BaseURL != "https://vidarchive.example.com" { |
| 41 | t.Errorf("expected base URL https://vidarchive.example.com, got %s", cfg.BaseURL) |
| 42 | } |
| 43 | if !cfg.IsHTTPS() { |
| 44 | t.Error("expected IsHTTPS() to be true") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestIsHTTPS(t *testing.T) { |
| 49 | tests := []struct { |
| 50 | url string |
| 51 | expected bool |
| 52 | }{ |
| 53 | {"", false}, |
| 54 | {"http://localhost:8080", false}, |
| 55 | {"https://example.com", true}, |
| 56 | {"https://vidarchive.example.com/path", true}, |
| 57 | } |
| 58 | |
| 59 | for _, test := range tests { |
| 60 | cfg := &Config{BaseURL: test.url} |
| 61 | if cfg.IsHTTPS() != test.expected { |
| 62 | t.Errorf("IsHTTPS() for %q = %v, expected %v", test.url, cfg.IsHTTPS(), test.expected) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 |