50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
t.Setenv("MESSENGER_HTTP_ADDR", "")
|
|
t.Setenv("MESSENGER_ENV", "")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if cfg.HTTPAddr != ":8080" {
|
|
t.Errorf("HTTPAddr = %q, want %q", cfg.HTTPAddr, ":8080")
|
|
}
|
|
if cfg.Environment != "development" {
|
|
t.Errorf("Environment = %q, want development", cfg.Environment)
|
|
}
|
|
}
|
|
|
|
func TestLoadFromEnv(t *testing.T) {
|
|
t.Setenv("MESSENGER_HTTP_ADDR", ":9000")
|
|
t.Setenv("MESSENGER_ENV", "production")
|
|
t.Setenv("MESSENGER_ALLOWED_ORIGINS", "https://a.example, https://b.example")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if cfg.HTTPAddr != ":9000" {
|
|
t.Errorf("HTTPAddr = %q, want :9000", cfg.HTTPAddr)
|
|
}
|
|
if cfg.Environment != "production" {
|
|
t.Errorf("Environment = %q, want production", cfg.Environment)
|
|
}
|
|
if len(cfg.AllowedOrigins) != 2 || cfg.AllowedOrigins[1] != "https://b.example" {
|
|
t.Errorf("AllowedOrigins = %v, want [https://a.example https://b.example]", cfg.AllowedOrigins)
|
|
}
|
|
}
|
|
|
|
func TestLoadInvalidEnv(t *testing.T) {
|
|
t.Setenv("MESSENGER_ENV", "nope")
|
|
|
|
if _, err := Load(); err == nil {
|
|
t.Fatal("Load() error = nil, want validation error")
|
|
}
|
|
}
|