-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc_router_test.go
More file actions
234 lines (216 loc) · 5.95 KB
/
Copy pathmc_router_test.go
File metadata and controls
234 lines (216 loc) · 5.95 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package mcrouterdiscovery
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewMcRouterClient(t *testing.T) {
client := NewMcRouterClient("http://localhost:8080", McRouterClientOpts{})
if client == nil {
t.Fatal("expected client to be non-nil")
}
if client.host != "http://localhost:8080" {
t.Errorf("expected host to be http://localhost:8080, got %s", client.host)
}
if client.client == nil {
t.Error("expected http client to be non-nil")
}
}
func TestGetRoutes(t *testing.T) {
tests := []struct {
name string
serverResponse *GetResponse
serverStatus int
expectError bool
}{
{
name: "successful get routes",
serverResponse: &GetResponse{
"server1.example.com": "backend1:25565",
"server2.example.com": "backend2:25565",
},
serverStatus: http.StatusOK,
expectError: false,
},
{
name: "empty routes",
serverResponse: &GetResponse{},
serverStatus: http.StatusOK,
expectError: false,
},
{
name: "server error",
serverResponse: nil,
serverStatus: http.StatusInternalServerError,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/routes" {
t.Errorf("expected path /routes, got %s", r.URL.Path)
}
if r.Method != http.MethodGet {
t.Errorf("expected method GET, got %s", r.Method)
}
w.WriteHeader(tt.serverStatus)
if tt.serverStatus == http.StatusOK && tt.serverResponse != nil {
json.NewEncoder(w).Encode(tt.serverResponse)
}
}))
defer server.Close()
client := NewMcRouterClient(server.URL, McRouterClientOpts{})
routes, err := client.GetRoutes()
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(routes) != len(*tt.serverResponse) {
t.Errorf("expected %d routes, got %d", len(*tt.serverResponse), len(routes))
}
for _, route := range routes {
expectedBackend, exists := (*tt.serverResponse)[route.ServerAddress]
if !exists {
t.Errorf("unexpected route with ServerAddress %s", route.ServerAddress)
}
if route.Backend != expectedBackend {
t.Errorf("expected Backend %s for ServerAddress %s, got %s", expectedBackend, route.ServerAddress, route.Backend)
}
}
}
})
}
}
func TestRegisterRoute(t *testing.T) {
tests := []struct {
name string
route Route
serverStatus int
expectError bool
}{
{
name: "successful registration",
route: Route{
ServerAddress: "server1.example.com",
Backend: "backend1:25565",
},
serverStatus: http.StatusOK,
expectError: false,
},
{
name: "successful registration with 201",
route: Route{
ServerAddress: "server2.example.com",
Backend: "backend2:25565",
},
serverStatus: http.StatusCreated,
expectError: false,
},
{
name: "server error",
route: Route{
ServerAddress: "server3.example.com",
Backend: "backend3:25565",
},
serverStatus: http.StatusInternalServerError,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var receivedRoute Route
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/routes" {
t.Errorf("expected path /routes, got %s", r.URL.Path)
}
if r.Method != http.MethodPost {
t.Errorf("expected method POST, got %s", r.Method)
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", r.Header.Get("Content-Type"))
}
if err := json.NewDecoder(r.Body).Decode(&receivedRoute); err != nil {
t.Errorf("failed to decode request body: %v", err)
}
w.WriteHeader(tt.serverStatus)
}))
defer server.Close()
client := NewMcRouterClient(server.URL, McRouterClientOpts{})
err := client.RegisterRoute(tt.route)
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if receivedRoute.ServerAddress != tt.route.ServerAddress {
t.Errorf("expected ServerAddress %s, got %s", tt.route.ServerAddress, receivedRoute.ServerAddress)
}
if receivedRoute.Backend != tt.route.Backend {
t.Errorf("expected Backend %s, got %s", tt.route.Backend, receivedRoute.Backend)
}
}
})
}
}
func TestDeleteRoute(t *testing.T) {
tests := []struct {
name string
serverAddress string
serverStatus int
expectError bool
}{
{
name: "successful deletion",
serverAddress: "server1.example.com",
serverStatus: http.StatusOK,
expectError: false,
},
{
name: "successful deletion with 204",
serverAddress: "server2.example.com",
serverStatus: http.StatusNoContent,
expectError: false,
},
{
name: "not found error",
serverAddress: "nonexistent.example.com",
serverStatus: http.StatusNotFound,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := "/routes/" + tt.serverAddress
if r.URL.Path != expectedPath {
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
}
if r.Method != http.MethodDelete {
t.Errorf("expected method DELETE, got %s", r.Method)
}
w.WriteHeader(tt.serverStatus)
}))
defer server.Close()
client := NewMcRouterClient(server.URL, McRouterClientOpts{})
err := client.DeleteRoute(tt.serverAddress)
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
})
}
}