setting_remote_routing_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  6. )
  7. func TestValidateRemoteRoutingURLSettings(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. value string
  11. want string
  12. wantError string
  13. }{
  14. {name: "valid HTTPS", value: " https://example.com/rules#fragment ", want: "https://example.com/rules"},
  15. {name: "credentials", value: "https://user:[email protected]/rules", wantError: "must not contain URL credentials"},
  16. {name: "missing host", value: "https:///rules", wantError: "absolute HTTPS URL"},
  17. {name: "legacy HTTP stays inline", value: "http://example.com/rules", want: "http://example.com/rules"},
  18. {name: "multiline Clash stays inline", value: "https://example.com/rules\nMATCH,PROXY", want: "https://example.com/rules\nMATCH,PROXY"},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. settings := &entity.AllSetting{SubRoutingRules: tt.value}
  23. err := validateSettingsURLs(settings)
  24. if tt.wantError != "" {
  25. if err == nil || !strings.Contains(err.Error(), tt.wantError) {
  26. t.Fatalf("err=%v, want %q", err, tt.wantError)
  27. }
  28. return
  29. }
  30. if err != nil || settings.SubRoutingRules != tt.want {
  31. t.Fatalf("value=%q err=%v", settings.SubRoutingRules, err)
  32. }
  33. })
  34. }
  35. }
  36. func TestValidateSubJsonRoutingRulesSetting(t *testing.T) {
  37. tests := []struct {
  38. name string
  39. value string
  40. want string
  41. wantError string
  42. }{
  43. {name: "remote URL is canonicalised", value: " https://example.com/DEFAULT.JSON#frag ", want: "https://example.com/DEFAULT.JSON"},
  44. {name: "remote URL with credentials is rejected", value: "https://user:[email protected]/DEFAULT.JSON", wantError: "JSON subscription routing source"},
  45. {name: "inline JSON passes through", value: "{\"DirectSites\":[\"geosite:cat\"]}", want: "{\"DirectSites\":[\"geosite:cat\"]}"},
  46. {name: "blank stays blank", value: "", want: ""},
  47. }
  48. for _, tt := range tests {
  49. t.Run(tt.name, func(t *testing.T) {
  50. settings := &entity.AllSetting{SubJsonRoutingRules: tt.value}
  51. err := validateSettingsURLs(settings)
  52. if tt.wantError != "" {
  53. if err == nil || !strings.Contains(err.Error(), tt.wantError) {
  54. t.Fatalf("err=%v, want %q", err, tt.wantError)
  55. }
  56. return
  57. }
  58. if err != nil || settings.SubJsonRoutingRules != tt.want {
  59. t.Fatalf("value=%q err=%v", settings.SubJsonRoutingRules, err)
  60. }
  61. })
  62. }
  63. }