|
1 | | -// internal/config/config_test.go |
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Pavan-Rana/rate-limiter/internal/limiter" |
| 9 | +) |
| 10 | + |
| 11 | +func TestLoad_Defaults(t *testing.T) { |
| 12 | + // Clear env to test fallbacks |
| 13 | + os.Unsetenv("REDIS_ADDR") |
| 14 | + os.Unsetenv("GRPC_ADDR") |
| 15 | + os.Unsetenv("HTTP_ADDR") |
| 16 | + |
| 17 | + cfg, err := Load() |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("expected no error, got %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + if cfg.RedisAddr != "localhost:6379" { |
| 23 | + t.Errorf("expected RedisAddr localhost:6379, got %s", cfg.RedisAddr) |
| 24 | + } |
| 25 | + |
| 26 | + if cfg.GRPCAddr != ":50051" { |
| 27 | + t.Errorf("expected GRPCAddr :50051, got %s", cfg.GRPCAddr) |
| 28 | + } |
| 29 | + |
| 30 | + if cfg.HTTPAddr != ":8080" { |
| 31 | + t.Errorf("expected HTTPAddr :8080, got %s", cfg.HTTPAddr) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestLoad_FromEnv(t *testing.T) { |
| 36 | + os.Setenv("REDIS_ADDR", "redis:1234") |
| 37 | + os.Setenv("GRPC_ADDR", ":9999") |
| 38 | + os.Setenv("HTTP_ADDR", ":7777") |
| 39 | + |
| 40 | + defer os.Unsetenv("REDIS_ADDR") |
| 41 | + defer os.Unsetenv("GRPC_ADDR") |
| 42 | + defer os.Unsetenv("HTTP_ADDR") |
| 43 | + |
| 44 | + cfg, err := Load() |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("expected no error, got %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + if cfg.RedisAddr != "redis:1234" { |
| 50 | + t.Errorf("expected RedisAddr redis:1234, got %s", cfg.RedisAddr) |
| 51 | + } |
| 52 | + |
| 53 | + if cfg.GRPCAddr != ":9999" { |
| 54 | + t.Errorf("expected GRPCAddr :9999, got %s", cfg.GRPCAddr) |
| 55 | + } |
| 56 | + |
| 57 | + if cfg.HTTPAddr != ":7777" { |
| 58 | + t.Errorf("expected HTTPAddr :7777, got %s", cfg.HTTPAddr) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestGetDefaultPolicy(t *testing.T) { |
| 63 | + cfg, _ := Load() |
| 64 | + |
| 65 | + p := cfg.GetDefault() |
| 66 | + |
| 67 | + if p.Limit != 100 { |
| 68 | + t.Errorf("expected limit 100, got %d", p.Limit) |
| 69 | + } |
| 70 | + |
| 71 | + if p.Window != time.Minute { |
| 72 | + t.Errorf("expected window 1 minute, got %v", p.Window) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestPolicyFor_NotFound(t *testing.T) { |
| 77 | + cfg, _ := Load() |
| 78 | + |
| 79 | + _, ok := cfg.PolicyFor("nonexistent") |
| 80 | + |
| 81 | + if ok { |
| 82 | + t.Errorf("expected policy to not exist") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestPolicyFor_Found(t *testing.T) { |
| 87 | + cfg, _ := Load() |
| 88 | + |
| 89 | + testPolicy := limiter.Policy{ |
| 90 | + Limit: 50, |
| 91 | + Window: time.Second * 30, |
| 92 | + } |
| 93 | + |
| 94 | + cfg.policies["test-key"] = testPolicy |
| 95 | + |
| 96 | + p, ok := cfg.PolicyFor("test-key") |
| 97 | + if !ok { |
| 98 | + t.Fatalf("expected policy to exist") |
| 99 | + } |
| 100 | + |
| 101 | + if p.Limit != 50 { |
| 102 | + t.Errorf("expected limit 50, got %d", p.Limit) |
| 103 | + } |
| 104 | + |
| 105 | + if p.Window != 30*time.Second { |
| 106 | + t.Errorf("expected window 30s, got %v", p.Window) |
| 107 | + } |
| 108 | +} |
0 commit comments