1
0

subClashService_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package sub
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestEnsureUniqueProxyNames(t *testing.T) {
  7. proxies := []map[string]any{
  8. {"name": "", "type": "vless", "server": "a.com", "port": 443},
  9. {"name": "", "type": "vmess", "server": "b.com", "port": 8443},
  10. {"name": "node"},
  11. {"name": "node"},
  12. {"name": ""},
  13. }
  14. ensureUniqueProxyNames(proxies)
  15. seen := map[string]bool{}
  16. for i, p := range proxies {
  17. name, _ := p["name"].(string)
  18. if name == "" {
  19. t.Fatalf("proxy %d still has an empty name (mihomo would reject the config, #4641)", i)
  20. }
  21. if seen[name] {
  22. t.Fatalf("proxy %d has duplicate name %q (mihomo rejects the whole config, #4641)", i, name)
  23. }
  24. seen[name] = true
  25. }
  26. if got := proxies[0]["name"]; got != "vless-a.com-443" {
  27. t.Errorf("empty name fallback = %q, want vless-a.com-443", got)
  28. }
  29. if proxies[2]["name"] == proxies[3]["name"] {
  30. t.Errorf("duplicate %q was not disambiguated", proxies[2]["name"])
  31. }
  32. if got := proxies[4]["name"]; got != "proxy-5" {
  33. t.Errorf("typeless empty name fallback = %q, want proxy-5", got)
  34. }
  35. }
  36. func TestApplyTransport_XHTTP(t *testing.T) {
  37. svc := &SubClashService{}
  38. proxy := map[string]any{}
  39. stream := map[string]any{
  40. "xhttpSettings": map[string]any{
  41. "path": "/xh",
  42. "host": "example.com",
  43. "mode": "auto",
  44. },
  45. }
  46. if !svc.applyTransport(proxy, "xhttp", stream) {
  47. t.Fatalf("applyTransport returned false for xhttp (#4531: would drop the inbound and yield an empty Clash YAML)")
  48. }
  49. if proxy["network"] != "xhttp" {
  50. t.Fatalf("network = %v, want xhttp", proxy["network"])
  51. }
  52. opts, ok := proxy["xhttp-opts"].(map[string]any)
  53. if !ok {
  54. t.Fatalf("xhttp-opts missing or wrong type: %#v", proxy["xhttp-opts"])
  55. }
  56. want := map[string]any{"path": "/xh", "host": "example.com", "mode": "auto"}
  57. if !reflect.DeepEqual(opts, want) {
  58. t.Fatalf("xhttp-opts = %#v, want %#v", opts, want)
  59. }
  60. }
  61. func TestApplyTransport_XHTTP_HostFromHeaders(t *testing.T) {
  62. svc := &SubClashService{}
  63. proxy := map[string]any{}
  64. stream := map[string]any{
  65. "xhttpSettings": map[string]any{
  66. "path": "/xh",
  67. "headers": map[string]any{"Host": "via-header.example.com"},
  68. },
  69. }
  70. if !svc.applyTransport(proxy, "xhttp", stream) {
  71. t.Fatalf("applyTransport returned false for xhttp")
  72. }
  73. opts, _ := proxy["xhttp-opts"].(map[string]any)
  74. if opts["host"] != "via-header.example.com" {
  75. t.Fatalf("host should fall back to headers.Host, got %v", opts["host"])
  76. }
  77. }
  78. func TestApplyTransport_HTTPUpgrade(t *testing.T) {
  79. svc := &SubClashService{}
  80. proxy := map[string]any{}
  81. stream := map[string]any{
  82. "httpupgradeSettings": map[string]any{
  83. "path": "/hu",
  84. "host": "example.com",
  85. },
  86. }
  87. if !svc.applyTransport(proxy, "httpupgrade", stream) {
  88. t.Fatalf("applyTransport returned false for httpupgrade")
  89. }
  90. if proxy["network"] != "httpupgrade" {
  91. t.Fatalf("network = %v, want httpupgrade", proxy["network"])
  92. }
  93. opts, ok := proxy["http-upgrade-opts"].(map[string]any)
  94. if !ok {
  95. t.Fatalf("http-upgrade-opts missing: %#v", proxy["http-upgrade-opts"])
  96. }
  97. if opts["path"] != "/hu" {
  98. t.Fatalf("path = %v, want /hu", opts["path"])
  99. }
  100. headers, _ := opts["headers"].(map[string]any)
  101. if headers["Host"] != "example.com" {
  102. t.Fatalf("headers.Host = %v, want example.com", headers["Host"])
  103. }
  104. }