test(handler/http): add 25+ tests for probe resistance, MITM, bypass, and edge cases

+698/-3 across auth_test, connect_test, handler_test, metadata_test,
proxy_test, and websocket_test. Covers probeResistanceResponse (web/file/
host/code types), handleRequest (bypass/UDP-disabled/CONNECT edge cases),
setupTrafficLimiter with observer, observeStats lifecycle, MITM metadata,
HTTP/1.0 keep-alive proxyRoundTrip, websocket body recording, and sniff
timeout handling. Fixes SetDeadline signatures from any→time.Time.
This commit is contained in:
ginuerzh
2026-05-30 17:54:27 +08:00
parent f8ddb193cb
commit a33c6f4a92
7 changed files with 698 additions and 3 deletions
+74
View File
@@ -8,6 +8,7 @@ import (
"github.com/go-gost/core/handler"
"github.com/go-gost/core/recorder"
xrecorder "github.com/go-gost/x/recorder"
)
func TestCopyWebsocketFrame_ReadError(t *testing.T) {
@@ -42,6 +43,79 @@ func TestCopyWebsocketDirection_ErrorReader(t *testing.T) {
}
}
func TestCopyWebsocketFrame_WithBodyRecording(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.recorder = recorder.RecorderObject{
Options: &recorder.Options{
HTTPBody: true,
MaxBodySize: 1024,
},
}
// Build a minimal WebSocket text frame (fin=1, opcode=1, payload="hello")
frame := []byte{0x81, 0x05, 'h', 'e', 'l', 'l', 'o'}
buf := &bytes.Buffer{}
ro := &xrecorder.HandlerRecorderObject{}
err := h.copyWebsocketFrame(io.Discard, bytes.NewReader(frame), buf, "client", ro)
if err != nil {
t.Fatalf("copyWebsocketFrame: %v", err)
}
if ro.Websocket == nil {
t.Fatal("expected Websocket recorder object")
}
if ro.Websocket.OpCode != 1 { // text frame
t.Errorf("got OpCode %d, want 1", ro.Websocket.OpCode)
}
if ro.InputBytes == 0 {
t.Error("expected non-zero InputBytes for client frame")
}
if ro.OutputBytes != 0 {
t.Error("expected zero OutputBytes for client frame")
}
}
func TestCopyWebsocketFrame_ServerDirection(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.recorder = recorder.RecorderObject{}
frame := []byte{0x82, 0x03, 'a', 'b', 'c'} // binary frame
buf := &bytes.Buffer{}
ro := &xrecorder.HandlerRecorderObject{}
err := h.copyWebsocketFrame(io.Discard, bytes.NewReader(frame), buf, "server", ro)
if err != nil {
t.Fatalf("copyWebsocketFrame: %v", err)
}
if ro.InputBytes != 0 {
t.Error("expected zero InputBytes for server frame")
}
if ro.OutputBytes == 0 {
t.Error("expected non-zero OutputBytes for server frame")
}
}
func TestCopyWebsocketDirection_NilRO(t *testing.T) {
h := &httpHandler{
options: handler.Options{
Logger: &testLogger{},
},
}
h.recorder = recorder.RecorderObject{}
errc := make(chan error, 1)
go h.copyWebsocketDirection(context.Background(), &errorReader{}, io.Discard, "client", nil, 10.0, &testLogger{}, errc)
<-errc
}
// errorReader always returns an error.
type errorReader struct{}