summaryrefslogtreecommitdiff
path: root/internal/handlers/claudomator_proxy_test.go
blob: bb2842a56ef2136758d3c103090ea1bb7e10a08f (plain)
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
173
174
175
package handlers

import (
	"bufio"
	"fmt"
	"net"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestClaudomatorProxy_HTTP_Forward(t *testing.T) {
	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "hello")
	}))
	defer backend.Close()

	proxy := NewClaudomatorProxy(backend.URL)

	req := httptest.NewRequest("GET", "/claudomator/", nil)
	rr := httptest.NewRecorder()
	proxy.ServeHTTP(rr, req)

	if rr.Code != http.StatusOK {
		t.Errorf("expected 200, got %d", rr.Code)
	}
	if body := rr.Body.String(); body != "hello" {
		t.Errorf("expected 'hello', got %q", body)
	}
}

func TestClaudomatorProxy_StripPrefix(t *testing.T) {
	var seenPath string
	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		seenPath = r.URL.Path
		w.WriteHeader(http.StatusOK)
	}))
	defer backend.Close()

	proxy := NewClaudomatorProxy(backend.URL)

	req := httptest.NewRequest("GET", "/claudomator/api/tasks", nil)
	rr := httptest.NewRecorder()
	proxy.ServeHTTP(rr, req)

	if seenPath != "/api/tasks" {
		t.Errorf("expected upstream to see /api/tasks, got %q", seenPath)
	}
}

func TestClaudomatorProxy_RawPathStrip(t *testing.T) {
	var seenRawPath string
	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		seenRawPath = r.URL.RawPath
		w.WriteHeader(http.StatusOK)
	}))
	defer backend.Close()

	proxy := NewClaudomatorProxy(backend.URL)

	req := httptest.NewRequest("GET", "/claudomator/api/tasks%2Ffoo", nil)
	rr := httptest.NewRecorder()
	proxy.ServeHTTP(rr, req)

	// RawPath should have /claudomator stripped
	if strings.HasPrefix(seenRawPath, "/claudomator") {
		t.Errorf("upstream still sees /claudomator prefix in RawPath: %q", seenRawPath)
	}
}

func TestClaudomatorProxy_ServiceWorkerHeader(t *testing.T) {
	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Service-Worker-Allowed", "/")
		w.WriteHeader(http.StatusOK)
	}))
	defer backend.Close()

	proxy := NewClaudomatorProxy(backend.URL)

	req := httptest.NewRequest("GET", "/claudomator/sw.js", nil)
	rr := httptest.NewRecorder()
	proxy.ServeHTTP(rr, req)

	if got := rr.Header().Get("Service-Worker-Allowed"); got != "/" {
		t.Errorf("expected Service-Worker-Allowed '/', got %q", got)
	}
}

// responseHijacker wraps httptest.ResponseRecorder and implements http.Hijacker
// so WebSocket hijack tests work without a real TCP connection.
type responseHijacker struct {
	*httptest.ResponseRecorder
	serverConn net.Conn
}

func (h *responseHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
	rw := bufio.NewReadWriter(bufio.NewReader(h.serverConn), bufio.NewWriter(h.serverConn))
	return h.serverConn, rw, nil
}

func TestClaudomatorProxy_WebSocket(t *testing.T) {
	// Stand up a minimal WebSocket echo backend that speaks raw HTTP upgrade
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer listener.Close()

	done := make(chan struct{})
	go func() {
		defer close(done)
		conn, err := listener.Accept()
		if err != nil {
			return
		}
		defer conn.Close()

		// Read the HTTP upgrade request from the proxy
		buf := make([]byte, 4096)
		n, err := conn.Read(buf)
		if err != nil || n == 0 {
			return
		}

		// Send a minimal HTTP 101 response so the proxy considers this a WS
		resp := "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n"
		_, _ = conn.Write([]byte(resp))

		// Echo one message: read "ping", write "pong"
		msgBuf := make([]byte, 64)
		n, _ = conn.Read(msgBuf)
		if n > 0 {
			_, _ = conn.Write(msgBuf[:n])
		}
	}()

	targetURL := "http://" + listener.Addr().String()

	proxy := NewClaudomatorProxy(targetURL)

	// Build a client-side TCP pipe to simulate the browser side
	clientSide, serverSide := net.Pipe()
	defer clientSide.Close()

	rr := httptest.NewRecorder()
	rh := &responseHijacker{ResponseRecorder: rr, serverConn: serverSide}

	req := httptest.NewRequest("GET", "/claudomator/ws", nil)
	req.Header.Set("Upgrade", "websocket")
	req.Header.Set("Connection", "Upgrade")

	proxyDone := make(chan struct{})
	go func() {
		defer close(proxyDone)
		proxy.ServeHTTP(rh, req)
	}()

	// Write a message from the "browser" side and read the echo
	_, err = clientSide.Write([]byte("ping"))
	if err != nil {
		t.Fatal(err)
	}

	// Read back — we expect "pong" from the echo server (or at least some data)
	got := make([]byte, 64)
	n, _ := clientSide.Read(got)
	if n == 0 {
		t.Error("expected data back from WebSocket echo")
	}

	clientSide.Close()
	<-proxyDone
	<-done
}