|
1 | 1 | package proxy
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + "time" |
4 | 12 |
|
5 |
| -// Stub test file - tests removed |
6 |
| -func TestStub(t *testing.T) { |
7 |
| - // This is a stub test |
8 |
| - t.Skip("stub test file") |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/coder/boundary/audit" |
| 16 | + "github.com/coder/boundary/rules" |
| 17 | +) |
| 18 | + |
| 19 | +// mockAuditor is a simple mock auditor for testing |
| 20 | +type mockAuditor struct{} |
| 21 | + |
| 22 | +func (m *mockAuditor) AuditRequest(req audit.Request) { |
| 23 | + // No-op for testing |
| 24 | +} |
| 25 | + |
| 26 | +// TestProxyServerBasicHTTP tests basic HTTP request handling |
| 27 | +func TestProxyServerBasicHTTP(t *testing.T) { |
| 28 | + // Create test logger |
| 29 | + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ |
| 30 | + Level: slog.LevelDebug, |
| 31 | + })) |
| 32 | + |
| 33 | + // Create test rules (allow all for testing) |
| 34 | + testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("Failed to parse test rules: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + // Create rule engine |
| 40 | + ruleEngine := rules.NewRuleEngine(testRules, logger) |
| 41 | + |
| 42 | + // Create mock auditor |
| 43 | + auditor := &mockAuditor{} |
| 44 | + |
| 45 | + // Create TLS config (minimal for testing) |
| 46 | + tlsConfig := &tls.Config{ |
| 47 | + MinVersion: tls.VersionTLS12, |
| 48 | + } |
| 49 | + |
| 50 | + // Create proxy server |
| 51 | + server := NewProxyServer(Config{ |
| 52 | + HTTPPort: 8080, |
| 53 | + RuleEngine: ruleEngine, |
| 54 | + Auditor: auditor, |
| 55 | + Logger: logger, |
| 56 | + TLSConfig: tlsConfig, |
| 57 | + }) |
| 58 | + |
| 59 | + // Create context with timeout |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + // Start server in goroutine |
| 64 | + serverDone := make(chan error, 1) |
| 65 | + go func() { |
| 66 | + serverDone <- server.Start(ctx) |
| 67 | + }() |
| 68 | + |
| 69 | + // Give server time to start |
| 70 | + time.Sleep(100 * time.Millisecond) |
| 71 | + |
| 72 | + // Test basic HTTP request |
| 73 | + t.Run("BasicHTTPRequest", func(t *testing.T) { |
| 74 | + // Create HTTP client |
| 75 | + client := &http.Client{ |
| 76 | + Transport: &http.Transport{ |
| 77 | + TLSClientConfig: &tls.Config{ |
| 78 | + InsecureSkipVerify: true, // Skip cert verification for testing |
| 79 | + }, |
| 80 | + }, |
| 81 | + Timeout: 5 * time.Second, |
| 82 | + } |
| 83 | + |
| 84 | + // Make request to proxy |
| 85 | + req, err := http.NewRequest("GET", "http://localhost:8080/todos/1", nil) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Failed to create request: %v", err) |
| 88 | + } |
| 89 | + // Override the Host header |
| 90 | + req.Host = "jsonplaceholder.typicode.com" |
| 91 | + |
| 92 | + // Make the request |
| 93 | + resp, err := client.Do(req) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + body, err := io.ReadAll(resp.Body) |
| 97 | + require.NoError(t, err) |
| 98 | + resp.Body.Close() |
| 99 | + |
| 100 | + expectedResponse := `{ |
| 101 | + "userId": 1, |
| 102 | + "id": 1, |
| 103 | + "title": "delectus aut autem", |
| 104 | + "completed": false |
| 105 | +}` |
| 106 | + require.Equal(t, expectedResponse, string(body)) |
| 107 | + }) |
9 | 108 | }
|
0 commit comments