-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend_test.go
More file actions
198 lines (157 loc) · 6.63 KB
/
backend_test.go
File metadata and controls
198 lines (157 loc) · 6.63 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
package reverseproxy
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
"github.com/GoCodeAlone/modular"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestStandaloneBackendProxyHandler tests the backend proxy handler directly without mocks
func TestStandaloneBackendProxyHandler(t *testing.T) {
// Create a direct handler function that simulates what backendProxyHandler should do
directHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Simulate the backend server response directly
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Server", "Backend1")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"server":"Backend1","path":"` + r.URL.Path + `"}`))
})
// Create a test request
req := httptest.NewRequest("GET", "/api/test", nil)
w := httptest.NewRecorder()
// Call the handler directly
directHandler.ServeHTTP(w, req)
// Get the response
resp := w.Result()
defer resp.Body.Close()
// Check status code
assert.Equal(t, http.StatusOK, resp.StatusCode)
// Check headers
assert.Equal(t, "Backend1", resp.Header.Get("X-Server"))
// Check body
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Contains(t, string(body), `"server":"Backend1"`)
}
// TestDefaultBackendRouting tests that requests without a specific matching route
// are correctly routed to the default backend
func TestDefaultBackendRouting(t *testing.T) {
// Create a new ReverseProxyModule instance
module := NewModule()
// Create a mock tenant application
mockApp := NewMockTenantApplication() // Changed from NewMockApplication()
// Register config with the module
err := module.RegisterConfig(mockApp)
require.NoError(t, err, "RegisterConfig should not fail")
// Initialize the module with the mock application
err = module.Init(mockApp) // Pass mockApp which is also a modular.Application
require.NoError(t, err, "Init should not fail")
// Setup backend servers
defaultBackendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Server", "DefaultBackend")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"server":"DefaultBackend","path":"` + r.URL.Path + `"}`))
}))
defer defaultBackendServer.Close()
specificBackendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Server", "SpecificBackend")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"server":"SpecificBackend","path":"` + r.URL.Path + `"}`))
}))
defer specificBackendServer.Close()
// Create test config with the mock servers
testConfig := &ReverseProxyConfig{
BackendServices: map[string]string{
"default": defaultBackendServer.URL,
"specific": specificBackendServer.URL,
},
DefaultBackend: "default",
}
// Create mock router
mockRouter := &testRouter{
routes: make(map[string]http.HandlerFunc),
}
// Set up module with test config
module.config = testConfig
module.defaultBackend = testConfig.DefaultBackend
module.router = mockRouter
module.httpClient = &http.Client{}
module.backendProxies = make(map[string]*httputil.ReverseProxy)
module.backendRoutes = make(map[string]map[string]http.HandlerFunc)
module.tenantBackendProxies = make(map[modular.TenantID]map[string]*httputil.ReverseProxy)
module.tenants = make(map[modular.TenantID]*ReverseProxyConfig)
module.compositeRoutes = make(map[string]http.HandlerFunc)
module.circuitBreakers = make(map[string]*CircuitBreaker)
// Initialize proxies for each backend
for backend, urlString := range testConfig.BackendServices {
backendURL, err := url.Parse(urlString)
require.NoError(t, err)
proxy := httputil.NewSingleHostReverseProxy(backendURL)
module.backendProxies[backend] = proxy
// Initialize route map for this backend
module.backendRoutes[backend] = make(map[string]http.HandlerFunc)
// Create a test tenant ID for tenant-specific proxies
tenantID := modular.TenantID("test-tenant")
if _, exists := module.tenantBackendProxies[tenantID]; !exists {
module.tenantBackendProxies[tenantID] = make(map[string]*httputil.ReverseProxy)
}
module.tenantBackendProxies[tenantID][backend] = proxy
// Register the default route handler for each backend
handler := func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
}
module.backendRoutes[backend]["/*"] = handler
}
// Add a specific route
err = module.AddBackendRoute("specific", "/api/specific/*")
if err != nil {
t.Fatalf("Failed to add backend route: %v", err)
}
// Start the module to set up routes including the default catch-all route
err = module.Start(context.Background())
require.NoError(t, err)
// Test 1: Request to specific route should go to the specific backend
specificHandler, exists := mockRouter.routes["/api/specific/*"]
require.True(t, exists, "Specific route should be registered")
specificReq := httptest.NewRequest("GET", "/api/specific/test", nil)
specificW := httptest.NewRecorder()
specificHandler(specificW, specificReq)
specificResp := specificW.Result()
defer specificResp.Body.Close()
assert.Equal(t, http.StatusOK, specificResp.StatusCode)
assert.Equal(t, "SpecificBackend", specificResp.Header.Get("X-Server"))
specificBody, err := io.ReadAll(specificResp.Body)
require.NoError(t, err)
assert.Contains(t, string(specificBody), `"server":"SpecificBackend"`)
// Test 2: Request to non-specific route should go to the default backend
defaultHandler, exists := mockRouter.routes["/*"]
require.True(t, exists, "Default route should be registered")
defaultReq := httptest.NewRequest("GET", "/some/random/path", nil)
defaultW := httptest.NewRecorder()
defaultHandler(defaultW, defaultReq)
defaultResp := defaultW.Result()
defer defaultResp.Body.Close()
assert.Equal(t, http.StatusOK, defaultResp.StatusCode)
assert.Equal(t, "DefaultBackend", defaultResp.Header.Get("X-Server"))
defaultBody, err := io.ReadAll(defaultResp.Body)
require.NoError(t, err)
assert.Contains(t, string(defaultBody), `"server":"DefaultBackend"`)
// Test 3: Request to root path should also go to the default backend
rootReq := httptest.NewRequest("GET", "/", nil)
rootW := httptest.NewRecorder()
defaultHandler(rootW, rootReq)
rootResp := rootW.Result()
defer rootResp.Body.Close()
assert.Equal(t, http.StatusOK, rootResp.StatusCode)
assert.Equal(t, "DefaultBackend", rootResp.Header.Get("X-Server"))
rootBody, err := io.ReadAll(rootResp.Body)
require.NoError(t, err)
assert.Contains(t, string(rootBody), `"server":"DefaultBackend"`)
}