subClashService_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package sub
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestApplyTransport_XHTTP(t *testing.T) {
  7. svc := &SubClashService{}
  8. proxy := map[string]any{}
  9. stream := map[string]any{
  10. "xhttpSettings": map[string]any{
  11. "path": "/xh",
  12. "host": "example.com",
  13. "mode": "auto",
  14. },
  15. }
  16. if !svc.applyTransport(proxy, "xhttp", stream) {
  17. t.Fatalf("applyTransport returned false for xhttp (#4531: would drop the inbound and yield an empty Clash YAML)")
  18. }
  19. if proxy["network"] != "xhttp" {
  20. t.Fatalf("network = %v, want xhttp", proxy["network"])
  21. }
  22. opts, ok := proxy["xhttp-opts"].(map[string]any)
  23. if !ok {
  24. t.Fatalf("xhttp-opts missing or wrong type: %#v", proxy["xhttp-opts"])
  25. }
  26. want := map[string]any{"path": "/xh", "host": "example.com", "mode": "auto"}
  27. if !reflect.DeepEqual(opts, want) {
  28. t.Fatalf("xhttp-opts = %#v, want %#v", opts, want)
  29. }
  30. }
  31. func TestApplyTransport_XHTTP_HostFromHeaders(t *testing.T) {
  32. svc := &SubClashService{}
  33. proxy := map[string]any{}
  34. stream := map[string]any{
  35. "xhttpSettings": map[string]any{
  36. "path": "/xh",
  37. "headers": map[string]any{"Host": "via-header.example.com"},
  38. },
  39. }
  40. if !svc.applyTransport(proxy, "xhttp", stream) {
  41. t.Fatalf("applyTransport returned false for xhttp")
  42. }
  43. opts, _ := proxy["xhttp-opts"].(map[string]any)
  44. if opts["host"] != "via-header.example.com" {
  45. t.Fatalf("host should fall back to headers.Host, got %v", opts["host"])
  46. }
  47. }
  48. func TestApplyTransport_HTTPUpgrade(t *testing.T) {
  49. svc := &SubClashService{}
  50. proxy := map[string]any{}
  51. stream := map[string]any{
  52. "httpupgradeSettings": map[string]any{
  53. "path": "/hu",
  54. "host": "example.com",
  55. },
  56. }
  57. if !svc.applyTransport(proxy, "httpupgrade", stream) {
  58. t.Fatalf("applyTransport returned false for httpupgrade")
  59. }
  60. if proxy["network"] != "httpupgrade" {
  61. t.Fatalf("network = %v, want httpupgrade", proxy["network"])
  62. }
  63. opts, ok := proxy["http-upgrade-opts"].(map[string]any)
  64. if !ok {
  65. t.Fatalf("http-upgrade-opts missing: %#v", proxy["http-upgrade-opts"])
  66. }
  67. if opts["path"] != "/hu" {
  68. t.Fatalf("path = %v, want /hu", opts["path"])
  69. }
  70. headers, _ := opts["headers"].(map[string]any)
  71. if headers["Host"] != "example.com" {
  72. t.Fatalf("headers.Host = %v, want example.com", headers["Host"])
  73. }
  74. }