Files
x/handler/http/connect_test.go
T
ginuerzh 3246b39c93 refactor(handler/http): split handler monolith into 7 focused files with 96 unit tests
Extract the 1024-line handler.go into concern-focused files:
  util.go       — 5 pure functions (decodeServerName, basicProxyAuth, upgradeType,
                  normalizeHostPort, buildConnectResponse) — 100% coverage
  auth.go       — authenticate (5 probe-resistance strategies + knock) + checkRateLimit
  connect.go    — handleConnect, sniffAndHandle, dial, setupTrafficLimiter
  proxy.go      — handleProxy keep-alive loop, proxyRoundTrip, handleUpgradeResponse
  websocket.go  — WebSocket frame sniffing/recording with rate-limited sampling
  udp.go        — UDP-over-HTTP relay with SOCKS5 tunnel
  metadata.go   — metadata struct, parseMetadata, probeResistance type

Key improvements:
- Transport injection (http.RoundTripper field) enables testing without real network
- Nil Router guards in dial, handleUDP, and nil Addr guard in checkRateLimit
- Pure functions converted from methods to package-level (no state dependency)
- setupTrafficLimiter returns cleanup closure to preserve defer lifetime
- handleConnect accepts caller's resp for correct recorder status capture
- Comprehensive package doc with full request-flow documentation

96 tests: 100% on pure functions, 86% on metadata parsing, 55% overall (integration
paths verified by e2e tests).
2026-05-28 23:06:37 +08:00

157 lines
3.3 KiB
Go

package http
import (
"context"
"io"
"net"
"net/http"
"testing"
"github.com/go-gost/core/handler"
xrecorder "github.com/go-gost/x/recorder"
)
func TestSniffAndHandle_NoMatch(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.md.sniffing = true
client, server := net.Pipe()
defer client.Close()
defer server.Close()
go func() {
client.Write([]byte("SSH-2.0-OpenSSH\r\n"))
}()
cc, _ := net.Pipe()
defer cc.Close()
handled, err := h.sniffAndHandle(context.Background(), server, cc, &xrecorder.HandlerRecorderObject{}, &testLogger{})
if err != nil {
t.Fatal(err)
}
if handled {
t.Error("expected not handled for non-HTTP/TLS traffic")
}
}
func TestDial_NilRouter(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
_, err := h.dial(context.Background(), "tcp", "example.com:80")
if err == nil {
t.Error("expected error from dial with nil router")
}
}
func TestSniffAndHandle_TLS(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.md.sniffing = true
client, server := net.Pipe()
defer client.Close()
defer server.Close()
// Write TLS ClientHello bytes (content type 0x16 = Handshake, followed by TLS version)
go func() {
client.Write([]byte{0x16, 0x03, 0x01, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05})
}()
cc, _ := net.Pipe()
defer cc.Close()
handled, err := h.sniffAndHandle(context.Background(), server, cc, &xrecorder.HandlerRecorderObject{}, &testLogger{})
if err != nil {
t.Logf("sniff error (expected without proper recorder): %v", err)
}
if !handled {
t.Error("expected handled for TLS traffic")
}
}
func TestHandleConnect_NilRouter(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.md.proxyAgent = defaultProxyAgent
h.md.readTimeout = 15
client, server := net.Pipe()
defer client.Close()
defer server.Close()
// Drain response to prevent Pipe blocking
go func() {
io.ReadAll(server)
}()
resp := testConnectResp()
err := h.handleConnect(context.Background(), client, &xrecorder.HandlerRecorderObject{}, &testLogger{}, "example.com:443", resp)
if err == nil {
t.Error("expected dial error with nil router")
}
}
func TestHandleConnect_SniffingEnabled(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.md.proxyAgent = defaultProxyAgent
h.md.readTimeout = 15
h.md.sniffing = true
client, server := net.Pipe()
defer client.Close()
defer server.Close()
// Drain response to prevent Pipe blocking
go func() {
io.ReadAll(server)
}()
resp := testConnectResp()
err := h.handleConnect(context.Background(), client, &xrecorder.HandlerRecorderObject{}, &testLogger{}, "example.com:443", resp)
if err == nil {
t.Error("expected dial error with nil router")
}
}
func testConnectResp() *http.Response {
return &http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
ContentLength: -1,
}
}
func TestDial_HostHash(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.md.hash = "host"
_, err := h.dial(context.Background(), "tcp", "example.com:80")
if err == nil {
t.Error("expected error from dial with nil router")
}
}