Skip to content

Commit 3ba9bc0

Browse files
Added basic HTTP test for proxy
1 parent 2ed7c35 commit 3ba9bc0

File tree

2 files changed

+110
-6
lines changed

2 files changed

+110
-6
lines changed

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ module github.com/coder/boundary
22

33
go 1.24
44

5-
require github.com/coder/serpent v0.10.0
5+
require (
6+
github.com/coder/serpent v0.10.0
7+
github.com/stretchr/testify v1.8.4
8+
)
69

710
require (
811
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6 // indirect
912
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
1013
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
14+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1115
github.com/hashicorp/errwrap v1.1.0 // indirect
1216
github.com/hashicorp/go-multierror v1.1.1 // indirect
1317
github.com/kr/text v0.2.0 // indirect
@@ -18,6 +22,7 @@ require (
1822
github.com/muesli/termenv v0.15.2 // indirect
1923
github.com/pion/transport/v2 v2.0.0 // indirect
2024
github.com/pion/udp v0.1.4 // indirect
25+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
2126
github.com/rivo/uniseg v0.4.4 // indirect
2227
github.com/spf13/pflag v1.0.5 // indirect
2328
go.opentelemetry.io/otel v1.19.0 // indirect

proxy/proxy_test.go

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,108 @@
11
package proxy
22

3-
import "testing"
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"io"
7+
"log/slog"
8+
"net/http"
9+
"os"
10+
"testing"
11+
"time"
412

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+
})
9108
}

0 commit comments

Comments
 (0)