-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnew_features_test.go
More file actions
529 lines (454 loc) · 16.7 KB
/
new_features_test.go
File metadata and controls
529 lines (454 loc) · 16.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package reverseproxy
import (
"context"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/GoCodeAlone/modular"
)
// TestNewFeatures tests the newly added features for debug endpoints and dry-run functionality
func TestNewFeatures(t *testing.T) {
// Create a logger for tests
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
t.Run("FileBasedFeatureFlagEvaluator_TenantAware", func(t *testing.T) {
// Create mock application with tenant support
app := NewMockTenantApplication()
config := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"global-flag": true,
"api-v2": false,
},
},
}
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(config))
// Register tenant with override configuration
tenantService := modular.NewStandardTenantService(logger)
if err := app.RegisterService("tenantService", tenantService); err != nil {
t.Fatalf("Failed to register tenant service: %v", err)
}
// Register tenant with specific config
tenantConfig := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"tenant-flag": true,
"global-flag": false, // Override global
},
},
}
err := tenantService.RegisterTenant("tenant-1", map[string]modular.ConfigProvider{
"reverseproxy": modular.NewStdConfigProvider(tenantConfig),
})
if err != nil {
t.Fatalf("Failed to register tenant: %v", err)
}
evaluator, err := NewFileBasedFeatureFlagEvaluator(context.Background(), app, logger)
if err != nil {
t.Fatalf("Failed to create feature flag evaluator: %v", err)
}
ctx := context.Background()
req := httptest.NewRequest("GET", "/test", nil)
// Test global flag evaluation
result, err := evaluator.EvaluateFlag(ctx, "global-flag", "", req)
if err != nil {
t.Errorf("Global flag evaluation failed: %v", err)
}
if result != true {
t.Errorf("Expected global flag to be true, got %v", result)
}
// Test tenant flag override
result, err = evaluator.EvaluateFlag(ctx, "global-flag", "tenant-1", req)
if err != nil {
t.Errorf("Tenant flag evaluation failed: %v", err)
}
if result != false {
t.Errorf("Expected tenant override to be false, got %v", result)
}
// Test tenant-specific flag
result, err = evaluator.EvaluateFlag(ctx, "tenant-flag", "tenant-1", req)
if err != nil {
t.Errorf("Tenant-specific flag evaluation failed: %v", err)
}
if result != true {
t.Errorf("Expected tenant flag to be true, got %v", result)
}
// Test unknown flag
result, err = evaluator.EvaluateFlag(ctx, "unknown-flag", "", req)
if err == nil {
t.Error("Expected error for unknown flag")
}
if result != false {
t.Errorf("Expected unknown flag to be false, got %v", result)
}
// Test EvaluateFlagWithDefault
result = evaluator.EvaluateFlagWithDefault(ctx, "missing-flag", "", req, true)
if result != true {
t.Errorf("Expected default value true for missing flag, got %v", result)
}
})
t.Run("DryRunHandler", func(t *testing.T) {
// Create mock backends
primaryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Backend", "primary")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"backend":"primary","message":"test"}`)); err != nil {
t.Errorf("Failed to write response: %v", err)
}
}))
defer primaryServer.Close()
secondaryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Backend", "secondary")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"backend":"secondary","message":"test"}`)); err != nil {
t.Errorf("Failed to write response: %v", err)
}
}))
defer secondaryServer.Close()
// Test dry-run disabled
disabledConfig := DryRunConfig{
Enabled: false,
}
disabledHandler := NewDryRunHandler(disabledConfig, "X-Tenant-ID", NewMockLogger())
req := httptest.NewRequest("GET", "/test", nil)
ctx := context.Background()
_, err := disabledHandler.ProcessDryRun(ctx, req, primaryServer.URL, secondaryServer.URL)
if err == nil {
t.Error("Expected error when dry-run is disabled")
}
if !errors.Is(err, ErrDryRunModeNotEnabled) {
t.Errorf("Expected ErrDryRunModeNotEnabled, got %v", err)
}
// Test dry-run enabled
enabledConfig := DryRunConfig{
Enabled: true,
LogResponses: true,
MaxResponseSize: 1024,
CompareHeaders: []string{"Content-Type", "X-Backend"},
IgnoreHeaders: []string{"Date"},
}
enabledHandler := NewDryRunHandler(enabledConfig, "X-Tenant-ID", NewMockLogger())
req = httptest.NewRequest("POST", "/test", strings.NewReader(`{"test":"data"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Request-ID", "test-123")
result, err := enabledHandler.ProcessDryRun(ctx, req, primaryServer.URL, secondaryServer.URL)
if err != nil {
t.Fatalf("Dry-run processing failed: %v", err)
}
if result == nil {
t.Fatal("Dry-run result is nil")
}
// Verify result structure
if result.PrimaryBackend != primaryServer.URL {
t.Errorf("Expected primary backend %s, got %s", primaryServer.URL, result.PrimaryBackend)
}
if result.SecondaryBackend != secondaryServer.URL {
t.Errorf("Expected secondary backend %s, got %s", secondaryServer.URL, result.SecondaryBackend)
}
if result.RequestID != "test-123" {
t.Errorf("Expected request ID 'test-123', got %s", result.RequestID)
}
if result.Method != "POST" {
t.Errorf("Expected method 'POST', got %s", result.Method)
}
// Verify responses were captured
if result.PrimaryResponse.StatusCode != http.StatusOK {
t.Errorf("Expected primary response status 200, got %d", result.PrimaryResponse.StatusCode)
}
if result.SecondaryResponse.StatusCode != http.StatusOK {
t.Errorf("Expected secondary response status 200, got %d", result.SecondaryResponse.StatusCode)
}
// Verify comparison was performed
if !result.Comparison.StatusCodeMatch {
t.Error("Expected status codes to match")
}
// Verify timing information
if result.Duration.Total == 0 {
t.Error("Expected total duration to be greater than 0")
}
})
t.Run("DebugHandler", func(t *testing.T) {
// Create mock application with feature flag configuration
app := NewMockTenantApplication()
config := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"test-flag": true,
"api-v2": false,
},
},
BackendServices: map[string]string{
"primary": "http://localhost:9001",
"secondary": "http://localhost:9002",
},
}
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(config))
// Create feature flag evaluator
evaluator, err := NewFileBasedFeatureFlagEvaluator(context.Background(), app, logger)
if err != nil {
t.Fatalf("Failed to create feature flag evaluator: %v", err)
}
// Update config with routes
config.Routes = map[string]string{
"/api/v1/*": "primary",
"/api/v2/*": "secondary",
}
config.DefaultBackend = "primary"
config.TenantIDHeader = "X-Tenant-ID"
config.RequireTenantID = false
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(config))
// Create debug handler
debugConfig := DebugEndpointsConfig{
Enabled: true,
BasePath: "/debug",
RequireAuth: false,
}
mockTenantService := &MockTenantService{}
debugHandler := NewDebugHandler(debugConfig, evaluator, config, mockTenantService, logger)
// Create test server
mux := http.NewServeMux()
debugHandler.RegisterRoutes(mux)
server := httptest.NewServer(mux)
defer server.Close()
// Test debug endpoints
endpoints := []struct {
path string
description string
}{
{"/debug/flags", "Feature flags endpoint"},
{"/debug/info", "General info endpoint"},
{"/debug/backends", "Backends endpoint"},
{"/debug/circuit-breakers", "Circuit breakers endpoint"},
{"/debug/health-checks", "Health checks endpoint"},
}
for _, endpoint := range endpoints {
t.Run(endpoint.description, func(t *testing.T) {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, "GET", server.URL+endpoint.path, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("X-Tenant-ID", "test-tenant")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
t.Errorf("Failed to close response body: %v", err)
}
}()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
// Verify content type
contentType := resp.Header.Get("Content-Type")
if !strings.Contains(contentType, "application/json") {
t.Errorf("Expected JSON content type, got: %s", contentType)
}
})
}
// Test authentication when required
t.Run("Authentication", func(t *testing.T) {
authDebugConfig := DebugEndpointsConfig{
Enabled: true,
BasePath: "/debug",
RequireAuth: true,
AuthToken: "test-token",
}
authDebugHandler := NewDebugHandler(authDebugConfig, evaluator, config, mockTenantService, logger)
authMux := http.NewServeMux()
authDebugHandler.RegisterRoutes(authMux)
authServer := httptest.NewServer(authMux)
defer authServer.Close()
// Test without auth token
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, "GET", authServer.URL+"/debug/flags", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
t.Errorf("Failed to close response body: %v", err)
}
}()
if resp.StatusCode != http.StatusUnauthorized {
t.Errorf("Expected status 401 without auth, got %d", resp.StatusCode)
}
ctx = context.Background()
// Test with correct auth token
req, err = http.NewRequestWithContext(ctx, "GET", authServer.URL+"/debug/flags", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Authorization", "Bearer test-token")
resp, err = client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
t.Errorf("Failed to close response body: %v", err)
}
}()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 with correct auth, got %d", resp.StatusCode)
}
// Test with incorrect auth token
req, err = http.NewRequestWithContext(ctx, "GET", authServer.URL+"/debug/flags", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Authorization", "Bearer wrong-token")
resp, err = client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
t.Errorf("Failed to close response body: %v", err)
}
}()
if resp.StatusCode != http.StatusForbidden {
t.Errorf("Expected status 403 with wrong auth, got %d", resp.StatusCode)
}
})
})
t.Run("ErrorHandling", func(t *testing.T) {
// Test static error definitions
if ErrDryRunModeNotEnabled == nil {
t.Error("ErrDryRunModeNotEnabled should be defined")
}
if ErrDryRunModeNotEnabled.Error() != "dry-run mode is not enabled" {
t.Errorf("Expected error message 'dry-run mode is not enabled', got '%s'", ErrDryRunModeNotEnabled.Error())
}
})
}
// TestScenarioIntegration tests integration of all new features
func TestScenarioIntegration(t *testing.T) {
// This test validates that all the new features work together
// as they would in the comprehensive testing scenarios example
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
// Create mock application with global feature flag configuration
app := NewMockTenantApplication()
config := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"toolkit-toolbox-api": false,
"oauth-token-api": false,
"oauth-introspect-api": false,
"test-dryrun-api": true,
},
},
}
app.RegisterConfigSection("reverseproxy", modular.NewStdConfigProvider(config))
// Create tenant service and register tenant with overrides
tenantService := modular.NewStandardTenantService(logger)
if err := app.RegisterService("tenantService", tenantService); err != nil {
t.Fatalf("Failed to register tenant service: %v", err)
}
// Register tenant with specific config (like sampleaff1 from scenarios)
tenantConfig := &ReverseProxyConfig{
FeatureFlags: FeatureFlagsConfig{
Enabled: true,
Flags: map[string]bool{
"toolkit-toolbox-api": false,
"oauth-token-api": true,
"oauth-introspect-api": true,
},
},
}
err := tenantService.RegisterTenant("sampleaff1", map[string]modular.ConfigProvider{
"reverseproxy": modular.NewStdConfigProvider(tenantConfig),
})
if err != nil {
t.Fatalf("Failed to register tenant: %v", err)
}
// Create feature flag evaluator with typical Chimera scenarios
_, err = NewFileBasedFeatureFlagEvaluator(context.Background(), app, logger) // Created for completeness but not used in this integration test
if err != nil {
t.Fatalf("Failed to create feature flag evaluator: %v", err)
}
// Test dry-run functionality with different backends
primaryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"backend":"chimera","endpoint":"toolkit-toolbox","feature_enabled":true}`)); err != nil {
t.Errorf("Failed to write response: %v", err)
}
}))
defer primaryServer.Close()
secondaryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"backend":"legacy","endpoint":"toolkit-toolbox","legacy_mode":true}`)); err != nil {
t.Errorf("Failed to write response: %v", err)
}
}))
defer secondaryServer.Close()
dryRunConfig := DryRunConfig{
Enabled: true,
LogResponses: true,
MaxResponseSize: 1048576,
CompareHeaders: []string{"Content-Type"},
IgnoreHeaders: []string{"Date", "X-Request-ID"},
DefaultResponseBackend: "secondary", // Test returning secondary response
}
dryRunHandler := NewDryRunHandler(dryRunConfig, "X-Affiliate-ID", logger)
dryRunReq := httptest.NewRequest("GET", "/api/v1/test/dryrun", nil)
dryRunReq.Header.Set("X-Affiliate-ID", "sampleaff1")
ctx := context.Background()
dryRunResult, err := dryRunHandler.ProcessDryRun(ctx, dryRunReq, primaryServer.URL, secondaryServer.URL)
if err != nil {
t.Fatalf("Dry-run processing failed: %v", err)
}
if dryRunResult == nil {
t.Fatal("Dry-run result is nil")
}
// Verify both backends were called and responses compared
if dryRunResult.PrimaryResponse.StatusCode != http.StatusOK {
t.Errorf("Expected primary response status 200, got %d", dryRunResult.PrimaryResponse.StatusCode)
}
if dryRunResult.SecondaryResponse.StatusCode != http.StatusOK {
t.Errorf("Expected secondary response status 200, got %d", dryRunResult.SecondaryResponse.StatusCode)
}
// Status codes should match
if !dryRunResult.Comparison.StatusCodeMatch {
t.Error("Expected status codes to match between backends")
}
// Test that the returned response indicates which backend was used
if dryRunResult.ReturnedResponse != "secondary" {
t.Errorf("Expected returned response to be 'secondary', got %s", dryRunResult.ReturnedResponse)
}
// Test the GetReturnedResponse method
returnedResponse := dryRunResult.GetReturnedResponse()
if returnedResponse.StatusCode != http.StatusOK {
t.Errorf("Expected returned response status 200, got %d", returnedResponse.StatusCode)
}
// Body content should be different (chimera vs legacy response)
if dryRunResult.Comparison.BodyMatch {
t.Error("Expected body content to differ between backends")
}
// Should have differences reported
if len(dryRunResult.Comparison.Differences) == 0 {
t.Error("Expected differences to be reported between backends")
}
t.Logf("Integration test completed successfully - all new features working together")
t.Logf("Feature flags evaluated, dry-run comparison completed with %d differences", len(dryRunResult.Comparison.Differences))
}