-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_url_edge_cases_test.go
More file actions
172 lines (159 loc) · 4.8 KB
/
Copy pathbackend_url_edge_cases_test.go
File metadata and controls
172 lines (159 loc) · 4.8 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
package proxyd
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
// TestBackendURLEdgeCases covers all the tricky slash/query edge cases
func TestBackendURLEdgeCases(t *testing.T) {
tests := []struct {
name string
baseURL string
method string
path string
query string
expected string
}{
// === SLASH EDGE CASES ===
{
name: "base URL with trailing slash + path",
baseURL: "http://backend:8080/",
method: "eth_sendRawTransaction",
path: "/fast",
query: "",
expected: "http://backend:8080/fast", // Should NOT be //fast
},
{
name: "base URL with trailing slash + path + query",
baseURL: "http://backend:8080/",
method: "eth_sendRawTransaction",
path: "/fast",
query: "hint=hash",
expected: "http://backend:8080/fast?hint=hash",
},
{
name: "path is just slash with query",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "/",
query: "hint=hash",
expected: "http://backend:8080?hint=hash", // Should NOT add slash before ?
},
// === QUERY STRING EDGE CASES ===
{
name: "multiple query parameters",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "/fast",
query: "hint=hash&builder=builder1&origin=wallet",
expected: "http://backend:8080/fast?hint=hash&builder=builder1&origin=wallet",
},
{
name: "query with URL-encoded special chars",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "/fast",
query: "hint=0x1234%20test&url=https%3A%2F%2Fexample.com",
expected: "http://backend:8080/fast?hint=0x1234%20test&url=https%3A%2F%2Fexample.com",
},
{
name: "query with equals in value",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "/fast",
query: "signature=0xabc=def",
expected: "http://backend:8080/fast?signature=0xabc=def",
},
// === EMPTY/MISSING EDGE CASES ===
{
name: "empty path string",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "",
query: "hint=hash",
expected: "http://backend:8080?hint=hash", // Empty path should be treated like "/"
},
{
name: "empty query string",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "/fast",
query: "",
expected: "http://backend:8080/fast", // Should NOT add ? for empty query
},
{
name: "both path and query empty",
baseURL: "http://backend:8080",
method: "eth_sendRawTransaction",
path: "",
query: "",
expected: "http://backend:8080",
},
// === METHOD FILTERING ===
{
name: "non-eth_sendRawTransaction ignores everything",
baseURL: "http://backend:8080",
method: "eth_call",
path: "/fast",
query: "hint=hash&builder=builder1",
expected: "http://backend:8080", // Should completely ignore path and query
},
// === PRODUCTION-LIKE SCENARIOS ===
{
name: "real production URL with everything",
baseURL: "http://rpc-endpoint.flashbots.svc.cluster.local:8080",
method: "eth_sendRawTransaction",
path: "/fast",
query: "hint=0xabcdef1234567890&builder=flashbots&origin=metamask",
expected: "http://rpc-endpoint.flashbots.svc.cluster.local:8080/fast?hint=0xabcdef1234567890&builder=flashbots&origin=metamask",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeyPath, tt.path)
ctx = context.WithValue(ctx, ContextKeyRawQuery, tt.query)
rpcReqs := []*RPCReq{{Method: tt.method}}
backendURL := buildBackendURL(tt.baseURL, rpcReqs, ctx)
assert.Equal(t, tt.expected, backendURL, "URL mismatch for case: %s", tt.name)
})
}
}
// TestBackendURLBoundaryConditions tests weird inputs that shouldn't happen but might
func TestBackendURLBoundaryConditions(t *testing.T) {
tests := []struct {
name string
baseURL string
rpcReqs []*RPCReq
path string
query string
expected string
}{
{
name: "empty rpcReqs array",
baseURL: "http://backend:8080",
rpcReqs: []*RPCReq{},
path: "/fast",
query: "hint=hash",
expected: "http://backend:8080", // Should not panic, just return base
},
{
name: "nil rpcReqs array",
baseURL: "http://backend:8080",
rpcReqs: nil,
path: "/fast",
query: "hint=hash",
expected: "http://backend:8080",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeyPath, tt.path)
ctx = context.WithValue(ctx, ContextKeyRawQuery, tt.query)
// Should not panic
backendURL := buildBackendURL(tt.baseURL, tt.rpcReqs, ctx)
assert.Equal(t, tt.expected, backendURL)
})
}
}