|
4 | 4 | "context" |
5 | 5 | "crypto/tls" |
6 | 6 | "fmt" |
7 | | - "io/ioutil" |
| 7 | + "io" |
8 | 8 | "log/slog" |
9 | 9 | "net/http" |
10 | 10 | "os" |
@@ -83,275 +83,28 @@ func TestProxyServerBasicHTTP(t *testing.T) { |
83 | 83 | } |
84 | 84 |
|
85 | 85 | // Make request to proxy |
86 | | - req, err := http.NewRequest("GET", "http://localhost:8080", nil) |
| 86 | + req, err := http.NewRequest("GET", "http://localhost:8080/todos/1", nil) |
87 | 87 | if err != nil { |
88 | 88 | t.Fatalf("Failed to create request: %v", err) |
89 | 89 | } |
90 | | - // Override the Host header to coder.com |
91 | | - req.Host = "coder.com" |
92 | | - //req.Header.Set("Host", "coder.com") |
93 | | - |
94 | | - // Set Host header (important for URL parsing) |
95 | | - //req.Host = "localhost:8080" |
| 90 | + // Override the Host header to jsonplaceholder.typicode.com |
| 91 | + req.Host = "jsonplaceholder.typicode.com" |
96 | 92 |
|
97 | 93 | // Make the request |
98 | 94 | resp, err := client.Do(req) |
99 | | - if err != nil { |
100 | | - t.Logf("Request failed (expected for proxy without target): %v", err) |
101 | | - // This is expected since we're not forwarding to a real target |
102 | | - } |
103 | | - //else { |
104 | | - // resp.Body.Close() |
105 | | - //} |
106 | | - fmt.Printf("err: %v\n", err) |
| 95 | + require.NoError(t, err) |
107 | 96 |
|
108 | | - body, err := ioutil.ReadAll(resp.Body) |
| 97 | + body, err := io.ReadAll(resp.Body) |
109 | 98 | require.NoError(t, err) |
110 | 99 | fmt.Printf("body: %s\n", body) |
111 | | - |
112 | 100 | resp.Body.Close() |
113 | | - }) |
114 | 101 |
|
115 | | - // Test CONNECT request |
116 | | - //t.Run("CONNECTRequest", func(t *testing.T) { |
117 | | - // // Create HTTP client |
118 | | - // client := &http.Client{ |
119 | | - // Transport: &http.Transport{ |
120 | | - // TLSClientConfig: &tls.Config{ |
121 | | - // InsecureSkipVerify: true, |
122 | | - // }, |
123 | | - // }, |
124 | | - // Timeout: 5 * time.Second, |
125 | | - // } |
126 | | - // |
127 | | - // // Make CONNECT request |
128 | | - // req, err := http.NewRequest("CONNECT", "http://example.com:443", nil) |
129 | | - // if err != nil { |
130 | | - // t.Fatalf("Failed to create CONNECT request: %v", err) |
131 | | - // } |
132 | | - // |
133 | | - // // Set Host header |
134 | | - // req.Host = "example.com:443" |
135 | | - // |
136 | | - // // Make the request |
137 | | - // resp, err := client.Do(req) |
138 | | - // if err != nil { |
139 | | - // t.Logf("CONNECT request failed (expected for proxy without target): %v", err) |
140 | | - // // This is expected since we're not forwarding to a real target |
141 | | - // } else { |
142 | | - // resp.Body.Close() |
143 | | - // } |
144 | | - //}) |
145 | | - // |
146 | | - //// Cancel context to stop server |
147 | | - //cancel() |
148 | | - // |
149 | | - //// Wait for server to stop |
150 | | - //select { |
151 | | - //case err := <-serverDone: |
152 | | - // if err != nil && err != context.Canceled { |
153 | | - // t.Errorf("Server stopped with error: %v", err) |
154 | | - // } |
155 | | - //case <-time.After(5 * time.Second): |
156 | | - // t.Error("Server did not stop within timeout") |
157 | | - //} |
| 102 | + expectedResponse := `{ |
| 103 | + "userId": 1, |
| 104 | + "id": 1, |
| 105 | + "title": "delectus aut autem", |
| 106 | + "completed": false |
| 107 | +}` |
| 108 | + require.Equal(t, expectedResponse, string(body)) |
| 109 | + }) |
158 | 110 | } |
159 | | - |
160 | | -// TestProxyServerWithRules tests proxy with specific rules |
161 | | -//func TestProxyServerWithRules(t *testing.T) { |
162 | | -// // Create test logger |
163 | | -// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
164 | | -// Level: slog.LevelError, |
165 | | -// })) |
166 | | -// |
167 | | -// // Create restrictive rules (only allow github.com) |
168 | | -// testRules, err := rules.ParseAllowSpecs([]string{"github.com"}) |
169 | | -// if err != nil { |
170 | | -// t.Fatalf("Failed to parse test rules: %v", err) |
171 | | -// } |
172 | | -// |
173 | | -// // Create rule engine |
174 | | -// ruleEngine := rules.NewRuleEngine(testRules, logger) |
175 | | -// |
176 | | -// // Create mock auditor |
177 | | -// auditor := &mockAuditor{} |
178 | | -// |
179 | | -// // Create TLS config |
180 | | -// tlsConfig := &tls.Config{ |
181 | | -// MinVersion: tls.VersionTLS12, |
182 | | -// } |
183 | | -// |
184 | | -// // Create proxy server |
185 | | -// server := NewProxyServer(Config{ |
186 | | -// HTTPPort: 0, // Use random port |
187 | | -// RuleEngine: ruleEngine, |
188 | | -// Auditor: auditor, |
189 | | -// Logger: logger, |
190 | | -// TLSConfig: tlsConfig, |
191 | | -// }) |
192 | | -// |
193 | | -// // Create context with timeout |
194 | | -// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
195 | | -// defer cancel() |
196 | | -// |
197 | | -// // Start server in goroutine |
198 | | -// serverDone := make(chan error, 1) |
199 | | -// go func() { |
200 | | -// serverDone <- server.Start(ctx) |
201 | | -// }() |
202 | | -// |
203 | | -// // Give server time to start |
204 | | -// time.Sleep(100 * time.Millisecond) |
205 | | -// |
206 | | -// // Test allowed request |
207 | | -// t.Run("AllowedRequest", func(t *testing.T) { |
208 | | -// client := &http.Client{ |
209 | | -// Transport: &http.Transport{ |
210 | | -// TLSClientConfig: &tls.Config{ |
211 | | -// InsecureSkipVerify: true, |
212 | | -// }, |
213 | | -// }, |
214 | | -// Timeout: 5 * time.Second, |
215 | | -// } |
216 | | -// |
217 | | -// req, err := http.NewRequest("GET", "http://localhost:8080/test", nil) |
218 | | -// if err != nil { |
219 | | -// t.Fatalf("Failed to create request: %v", err) |
220 | | -// } |
221 | | -// |
222 | | -// // Set Host header to github.com (should be allowed) |
223 | | -// req.Host = "github.com" |
224 | | -// |
225 | | -// resp, err := client.Do(req) |
226 | | -// if err != nil { |
227 | | -// t.Logf("Request failed (expected for proxy without target): %v", err) |
228 | | -// } else { |
229 | | -// resp.Body.Close() |
230 | | -// } |
231 | | -// }) |
232 | | -// |
233 | | -// // Test blocked request |
234 | | -// t.Run("BlockedRequest", func(t *testing.T) { |
235 | | -// client := &http.Client{ |
236 | | -// Transport: &http.Transport{ |
237 | | -// TLSClientConfig: &tls.Config{ |
238 | | -// InsecureSkipVerify: true, |
239 | | -// }, |
240 | | -// }, |
241 | | -// Timeout: 5 * time.Second, |
242 | | -// } |
243 | | -// |
244 | | -// req, err := http.NewRequest("GET", "http://localhost:8080/test", nil) |
245 | | -// if err != nil { |
246 | | -// t.Fatalf("Failed to create request: %v", err) |
247 | | -// } |
248 | | -// |
249 | | -// // Set Host header to example.com (should be blocked) |
250 | | -// req.Host = "example.com" |
251 | | -// |
252 | | -// resp, err := client.Do(req) |
253 | | -// if err != nil { |
254 | | -// t.Logf("Request failed (expected for proxy without target): %v", err) |
255 | | -// } else { |
256 | | -// resp.Body.Close() |
257 | | -// } |
258 | | -// }) |
259 | | -// |
260 | | -// // Cancel context to stop server |
261 | | -// cancel() |
262 | | -// |
263 | | -// // Wait for server to stop |
264 | | -// select { |
265 | | -// case err := <-serverDone: |
266 | | -// if err != nil && err != context.Canceled { |
267 | | -// t.Errorf("Server stopped with error: %v", err) |
268 | | -// } |
269 | | -// case <-time.After(5 * time.Second): |
270 | | -// t.Error("Server did not stop within timeout") |
271 | | -// } |
272 | | -//} |
273 | | -// |
274 | | -//// TestProxyServerStartStop tests basic start/stop functionality |
275 | | -//func TestProxyServerStartStop(t *testing.T) { |
276 | | -// // Create test logger |
277 | | -// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
278 | | -// Level: slog.LevelError, |
279 | | -// })) |
280 | | -// |
281 | | -// // Create test rules |
282 | | -// testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
283 | | -// if err != nil { |
284 | | -// t.Fatalf("Failed to parse test rules: %v", err) |
285 | | -// } |
286 | | -// |
287 | | -// ruleEngine := rules.NewRuleEngine(testRules, logger) |
288 | | -// auditor := &mockAuditor{} |
289 | | -// tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} |
290 | | -// |
291 | | -// // Create proxy server |
292 | | -// server := NewProxyServer(Config{ |
293 | | -// HTTPPort: 0, |
294 | | -// RuleEngine: ruleEngine, |
295 | | -// Auditor: auditor, |
296 | | -// Logger: logger, |
297 | | -// TLSConfig: tlsConfig, |
298 | | -// }) |
299 | | -// |
300 | | -// // Test that server can be created |
301 | | -// if server == nil { |
302 | | -// t.Fatal("Failed to create proxy server") |
303 | | -// } |
304 | | -// |
305 | | -// // Test that server can be stopped (even if not started) |
306 | | -// err = server.Stop() |
307 | | -// if err != nil { |
308 | | -// t.Errorf("Stop() failed: %v", err) |
309 | | -// } |
310 | | -//} |
311 | | -// |
312 | | -//// TestProxyServerContextCancellation tests context cancellation |
313 | | -//func TestProxyServerContextCancellation(t *testing.T) { |
314 | | -// // Create test logger |
315 | | -// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
316 | | -// Level: slog.LevelError, |
317 | | -// })) |
318 | | -// |
319 | | -// // Create test rules |
320 | | -// testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
321 | | -// if err != nil { |
322 | | -// t.Fatalf("Failed to parse test rules: %v", err) |
323 | | -// } |
324 | | -// |
325 | | -// ruleEngine := rules.NewRuleEngine(testRules, logger) |
326 | | -// auditor := &mockAuditor{} |
327 | | -// tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} |
328 | | -// |
329 | | -// // Create proxy server |
330 | | -// server := NewProxyServer(Config{ |
331 | | -// HTTPPort: 0, |
332 | | -// RuleEngine: ruleEngine, |
333 | | -// Auditor: auditor, |
334 | | -// Logger: logger, |
335 | | -// TLSConfig: tlsConfig, |
336 | | -// }) |
337 | | -// |
338 | | -// // Create context that will be cancelled quickly |
339 | | -// ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
340 | | -// defer cancel() |
341 | | -// |
342 | | -// // Start server |
343 | | -// serverDone := make(chan error, 1) |
344 | | -// go func() { |
345 | | -// serverDone <- server.Start(ctx) |
346 | | -// }() |
347 | | -// |
348 | | -// // Wait for context cancellation |
349 | | -// select { |
350 | | -// case err := <-serverDone: |
351 | | -// if err != nil && err != context.DeadlineExceeded { |
352 | | -// t.Errorf("Server stopped with unexpected error: %v", err) |
353 | | -// } |
354 | | -// case <-time.After(1 * time.Second): |
355 | | -// t.Error("Server did not stop within timeout") |
356 | | -// } |
357 | | -//} |
0 commit comments