Add WebSocket upgrade handling and tests in Mihomo
Publish telemt-api gateway Docker image / test (push) Successful in 26s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m0s

- Implemented enhanced logic in `isWebSocketUpgrade` to accurately determine WebSocket upgrade requests by checking both "Connection" and "Upgrade" headers.
- Added a new test function `TestIsWebSocketUpgrade` to validate the WebSocket upgrade detection logic, ensuring correct behavior for various header configurations.
This commit is contained in:
Denozordec
2026-03-31 10:22:37 +07:00
parent 6c1f8a8a37
commit 1686840b0e
2 changed files with 30 additions and 1 deletions
+15
View File
@@ -39,3 +39,18 @@ func TestMihomoForwardRewritesPathAndAuth(t *testing.T) {
}
assertSameURL(t, cap.got.URL, want)
}
func TestIsWebSocketUpgrade(t *testing.T) {
r1 := httptest.NewRequest(http.MethodGet, "http://gw/api/mtg/mihomo/traffic", nil)
r1.Header.Set("Connection", "keep-alive, Upgrade")
r1.Header.Set("Upgrade", "websocket")
if !isWebSocketUpgrade(r1) {
t.Fatal("expected websocket upgrade for tokenized headers")
}
r2 := httptest.NewRequest(http.MethodGet, "http://gw/api/mtg/mihomo/traffic", nil)
r2.Header.Set("Upgrade", "websocket")
if isWebSocketUpgrade(r2) {
t.Fatal("expected non-websocket when Connection lacks upgrade")
}
}