-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter_test.go
More file actions
89 lines (76 loc) · 1.99 KB
/
router_test.go
File metadata and controls
89 lines (76 loc) · 1.99 KB
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
package pgmux
import (
"context"
"testing"
)
func TestStaticRouter(t *testing.T) {
ctx := context.Background()
t.Run("empty router", func(t *testing.T) {
router := NewStaticRouter(nil)
_, err := router.Route(ctx, "any_user")
if err != ErrUserNotFound {
t.Errorf("expected ErrUserNotFound, got %v", err)
}
})
t.Run("basic routing", func(t *testing.T) {
mappings := map[string]*BackendConfig{
"user1": {
Host: "host1.example.com",
Port: 5432,
User: "backend_user1",
},
"user2": {
Host: "host2.example.com",
Port: 5433,
User: "backend_user2",
},
}
router := NewStaticRouter(mappings)
// Test user1
config, err := router.Route(ctx, "user1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.Host != "host1.example.com" || config.Port != 5432 || config.User != "backend_user1" {
t.Errorf("unexpected config: %+v", config)
}
// Test user2
config, err = router.Route(ctx, "user2")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.Host != "host2.example.com" || config.Port != 5433 || config.User != "backend_user2" {
t.Errorf("unexpected config: %+v", config)
}
// Test unknown user
_, err = router.Route(ctx, "unknown")
if err != ErrUserNotFound {
t.Errorf("expected ErrUserNotFound, got %v", err)
}
})
t.Run("add and remove mappings", func(t *testing.T) {
router := NewStaticRouter(nil)
// Add mapping
config := &BackendConfig{
Host: "new.example.com",
Port: 5432,
User: "new_backend",
}
router.AddMapping("new_user", config)
// Verify it exists
result, err := router.Route(ctx, "new_user")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != config {
t.Error("expected same config pointer")
}
// Remove mapping
router.RemoveMapping("new_user")
// Verify it's gone
_, err = router.Route(ctx, "new_user")
if err != ErrUserNotFound {
t.Errorf("expected ErrUserNotFound after removal, got %v", err)
}
})
}