From a534463c18d085e1c22eb2833a274e80d8802f68 Mon Sep 17 00:00:00 2001 From: kazeyukiro Date: Wed, 7 Oct 2026 16:47:54 +0000 Subject: [PATCH] test(listener): allow HTTP/SOCKS; keep excluding tun/tproxy/mixed --- backend/internal/listener/listener_test.go | 19 +++++++++++++++++-- .../config/generator_listener_schema_test.go | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/backend/internal/listener/listener_test.go b/backend/internal/listener/listener_test.go index d0a5e41..a22d467 100644 --- a/backend/internal/listener/listener_test.go +++ b/backend/internal/listener/listener_test.go @@ -48,9 +48,24 @@ func TestGenerateConfigYAML(t *testing.T) { } func TestGenerateConfigYAMLRejectsExcludedProtocol(t *testing.T) { - _, err := listener.GenerateConfigYAML([]models.Listener{{Name: "socks", Type: "socks", Protocol: "socks", Listen: "0.0.0.0", Port: "1080", Enabled: true}}) + _, err := listener.GenerateConfigYAML([]models.Listener{{Name: "tun-bad", Type: "tun", Protocol: "tun", Listen: "0.0.0.0", Port: "1080", Enabled: true}}) if err == nil { - t.Fatal("expected excluded SOCKS listener protocol to be rejected") + t.Fatal("expected excluded TUN listener protocol to be rejected") + } +} + +func TestGenerateConfigYAMLAcceptsHTTPAndSOCKS(t *testing.T) { + for _, protocol := range []string{"http", "socks"} { + yamlStr, err := listener.GenerateConfigYAML([]models.Listener{{ + Name: "proxy-" + protocol, Type: protocol, Protocol: protocol, + Listen: "0.0.0.0", Port: "1080", Enabled: true, UDP: protocol == "socks", + }}) + if err != nil { + t.Fatalf("%s: %v", protocol, err) + } + if !contains(yamlStr, "type: "+protocol) { + t.Fatalf("%s: expected type in yaml, got %q", protocol, yamlStr) + } } } diff --git a/backend/internal/mihomo/config/generator_listener_schema_test.go b/backend/internal/mihomo/config/generator_listener_schema_test.go index 37d8e4f..d772487 100644 --- a/backend/internal/mihomo/config/generator_listener_schema_test.go +++ b/backend/internal/mihomo/config/generator_listener_schema_test.go @@ -116,8 +116,22 @@ func TestGenerateShadowQUICUsesCredentialsAsList(t *testing.T) { } } +func TestGenerateListenersAcceptsHTTPAndSOCKS(t *testing.T) { + for _, protocol := range []string{"http", "socks"} { + result, err := generateListeners(nil, []models.Listener{{ + Name: "p-" + protocol, Protocol: protocol, Port: "1080", Enabled: true, UDP: protocol == "socks", + }}, nil) + if err != nil { + t.Fatalf("%s: %v", protocol, err) + } + if len(result) != 1 || result[0]["type"] != protocol { + t.Fatalf("%s: unexpected result %#v", protocol, result) + } + } +} + func TestGenerateListenersRejectsExcludedProtocols(t *testing.T) { - for _, protocol := range []string{"socks", "http", "tproxy", "redir", "mixed", "tunnel", "tun", "wireguard"} { + for _, protocol := range []string{"tproxy", "redir", "mixed", "tunnel", "tun", "wireguard"} { _, err := generateListeners(nil, []models.Listener{{Name: "bad", Protocol: protocol, Port: "1080", Enabled: true}}, nil) if err == nil { t.Fatalf("expected protocol %q to be rejected", protocol)