-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
123 lines (100 loc) · 3.02 KB
/
Copy pathmiddleware_test.go
File metadata and controls
123 lines (100 loc) · 3.02 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
package dbresolver
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHTTPMiddleware(t *testing.T) {
// Create a mock DB
primary := MockDB()
replica := MockDB()
// Create DB with causal consistency
config := &CausalConsistencyConfig{
Enabled: true,
Level: ReadYourWrites,
FallbackToMaster: true,
}
db := New(
WithPrimaryDBs(primary),
WithReplicaDBs(replica),
WithCausalConsistencyConfig(config),
)
// Create a simple router for testing
router := NewSimpleRouter(db)
// Create middleware
middleware := NewHTTPMiddleware(router, "test_lsn", 0, false)
// Create a test handler that simulates a write operation
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
// Wrap the handler with middleware
wrappedHandler := middleware.Middleware(testHandler)
// Create a test request
req := httptest.NewRequest("GET", "/", http.NoBody)
rec := httptest.NewRecorder()
// Serve the request
wrappedHandler.ServeHTTP(rec, req)
// Check response
resp := rec.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
// Note: Since we're not actually doing a write operation in this test,
// we don't expect a cookie to be set. The test verifies that the middleware
// properly wraps the response writer.
}
func TestHTTPMiddlewareWithExistingLSNCookie(t *testing.T) {
// Create a mock DB
primary := MockDB()
replica := MockDB()
// Create DB with causal consistency
config := &CausalConsistencyConfig{
Enabled: true,
Level: ReadYourWrites,
FallbackToMaster: true,
}
db := New(
WithPrimaryDBs(primary),
WithReplicaDBs(replica),
WithCausalConsistencyConfig(config),
)
// Create a simple router for testing
router := NewSimpleRouter(db)
// Create middleware
middleware := NewHTTPMiddleware(router, "test_lsn", 0, false)
// Create a test handler
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if LSN context is present and has the correct LSN
ctx := r.Context()
lsnCtx := GetLSNContext(ctx)
if lsnCtx == nil {
t.Error("LSN context should be present")
} else if lsnCtx.RequiredLSN.IsZero() {
t.Error("Expected LSN in context to be set from cookie")
} else {
lsnStr := lsnCtx.RequiredLSN.String()
if lsnStr != "1/ABCDEF" {
t.Errorf("Expected LSN '1/ABCDEF' in context, got '%s'", lsnStr)
}
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
// Wrap the handler with middleware
wrappedHandler := middleware.Middleware(testHandler)
// Create a test request with existing LSN cookie
req := httptest.NewRequest("GET", "/", http.NoBody)
req.AddCookie(&http.Cookie{
Name: "test_lsn",
Value: "1/ABCDEF",
})
rec := httptest.NewRecorder()
// Serve the request
wrappedHandler.ServeHTTP(rec, req)
// Check response
resp := rec.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
}